Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Latest commit

 

History

History
186 lines (152 loc) · 7 KB

README.md

File metadata and controls

186 lines (152 loc) · 7 KB

Laravel URL Aliases

License Build Status Latest Stable Version Total Downloads Quality Score

Installation

  1. Require this package with composer
composer require fomvasss/laravel-url-aliases
  1. Publish package resource:
php artisan vendor:publish --provider="Fomvasss\UrlAliases\ServiceProvider"
  • config
  • migration
  1. Run migrate:
php artisan migrate

Integration

  1. Add to your model next trait: Fomvasss\UrlAliases\Traits\UrlAliasable

This trait have next relation-method:

  • urlAlias() - related UrlAlias model

Do not forget use with('urlAlias') in your models when you get list!

  1. Add the middleware to Http/Kernel.php:
    protected $middleware = [
        //...
        \Fomvasss\UrlAliases\Middleware\UrlAliasMiddleware::class,
    ];

Usage

Facade

  • \Fomvasss\UrlAliases\Facades\UrlAlias::route('article.show', $article)
  • \Fomvasss\UrlAliases\Facades\UrlAlias::current()

Helper functions

  • route_alias() - works the same way as Laravel helper route()
  • url_alias_current() - return alias path (or system path if alias not exists)
  • prepare_url_path() - return path for URL: https://your-site.com/my-first-page/example/ -> my-first-page/example

Examples usage

  • routes/web.php:
Route::group(['namespace' => 'Front'], function () {
    //...
    Route::get('article', 'ArticleController@index')->name('article.index');
    Route::get('article/{id}', 'ArticleController@show')->name('article.show');
    Route::post('article', 'ArticleController@store')->name('article.store');
	//...
});
  • app/Http/Controllers/Front/ArticleController.php:
public function index(Request $request)
{
    $articles = \App\Models\Article::paginate($request->per_page);
    
    // foreach($articles as $article) {
    //	 dump(route_alias('article.show', $article));
    // }
    
    return view('article.index', compact('articles'));
}

public function store(Request $request)
{
    $article = \App\Models\Article::create($request->only([
        //...
    ]);
    
    // 1) Make alias for system route:
    $article->urlAlias()->create([
        'source' => trim(route('article.show', $article, false), '/'),      // Ex.: system/article/26
        'alias' => str_slug($article->title).'/'.str_slug($article->user->name), // must be unique! Ex.: my-first-article/taylor-otwell
    ]); 
        
    // 2) Or custom redirection:
    $article->urlAlias()->create([
        'source' => 'about',
        'alias' => 'page/about'
        'type' => 301, // Status Code
    ]);
    
	// 3) Or if external link:
	$article->urlAlias()->create([
		'source' => 'https://google.com.ua',
		'alias' => 'my-google'
		'type' => 302, // Status Code
	]);

    return redirect()->route('article.index');
}

public function show(Request $request, $id)
{
    $article = \App\Models\Article::findOrFail($id);

    // dump($article->urlAlias);
    // dump($article->urlA());
   
    return view('article.show', compact('article'));
}
<li><a href="{{ route('article.show', $article->id) }}">System Link - 301 redirect to alias (if exists)</a></li>
<li><a href="{{ request()->path() }}">System path - redirect to alias (if exists)</a></li>
<li><a href="{{ route_alias('article.index', ['page' => '3', 'per_page' => 15]) }}">All articles</a></li>
<li><a href="{{ route_alias('article.show', [$article, 'page' => '3', 'per_page' => 15]) }}">Alias Link to article - absolute path</a></li>
<li><a href="{{ route_alias('article.show', $article, false) }}">Alias Link to article - relative path</a></li>
<li><a href="{{ route_alias('article.show', ['page' => '3', 'per_page' => 15]) }}">System Link - if not exist alias</a></li>
<li><a href="{{ \Fomvasss\UrlAliases\Facades\UrlAlias::route('article.show', $article) }}">Alias Link to article - absolute path</a></li>
<li><a href="{{ \Fomvasss\UrlAliases\Facades\UrlAlias::current() }}">Current path (alias or system)</a></li>

In UrlAlias::current() (route_alias()) second argument (if array - first index) may be id or the instanceof \Illuminate\Database\Eloquent\Model (like route Laravel helper)


Use localization URL's (dev)

For use localization url's, you need do next steps:

  1. Add to Http/Kernel.php next middleware:
    protected $routeMiddleware = [
        //...
        'applyUrlLocaleToRootPage' => \Fomvasss\UrlAliases\Middleware\ApplyUrlLocaleToRootPage::class,
    ];
  1. Set in config/url-aliases.php: 'use_localization' => true,
  2. Uncomment needed locales in config/url-aliases-laravellocalization.php and set other params
  3. Make or change your home page (root) routes, for example:
Route::get('/{locale?}', function () {
    return view('home');
})->name('home')->middleware('applyUrlLocaleToRootPage');
  1. Save aliases for entity and set locale:
    $article->urlAlias()->create([
        'source' => trim(route('system.article.show', $article, false), '/'),		// Ex.: system/article/26
        'alias' => str_slug($article->title).'/'.str_slug($article->user->name),	// Must be unique! Ex.: my-first-article/taylor-otwell
        'locale' => 'en',
        'locale_bound' => 123,                                                      // for related locale aliases
    ]);
  1. Use facade UrlAliasLocalization and next methods (like in mcamara/laravel-localization):
    UrlAliasLocalization::getDefaultLocale()
    UrlAliasLocalization::getCurrentLocale()
    UrlAliasLocalization::getCurrentLocaleName()
    UrlAliasLocalization::getCurrentLocaleNative()
    UrlAliasLocalization::getCurrentLocaleNativeReading()
    UrlAliasLocalization::getCurrentLocaleRegional()
    UrlAliasLocalization::getCurrentLocaleDirection()
    UrlAliasLocalization::getCurrentLocaleScript()
    UrlAliasLocalization::getLocalesOrder()
    UrlAliasLocalization::getSupportedLocales()
    UrlAliasLocalization::getSupportedLanguagesKeys()
    UrlAliasLocalization::getRoot() // http://site.com/ua, http://site.com/de 
    UrlAliasLocalization::getCurrentBound() // Get locales and links to related locale aliases 
    UrlAliasLocalization::getLocaleModelBound()
    UrlAliasLocalization::getLocalesModelsBound()

Links