- Require this package with composer
composer require fomvasss/laravel-url-aliases
- Publish package resource:
php artisan vendor:publish --provider="Fomvasss\UrlAliases\ServiceProvider"
- config
- migration
- Run migrate:
php artisan migrate
- 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!
- Add the middleware to
Http/Kernel.php
:
protected $middleware = [
//...
\Fomvasss\UrlAliases\Middleware\UrlAliasMiddleware::class,
];
\Fomvasss\UrlAliases\Facades\UrlAlias::route('article.show', $article)
\Fomvasss\UrlAliases\Facades\UrlAlias::current()
route_alias()
- works the same way as Laravel helperroute()
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
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 beid
or the instanceof\Illuminate\Database\Eloquent\Model
(likeroute
Laravel helper)
For use localization url's, you need do next steps:
- Add to
Http/Kernel.php
next middleware:
protected $routeMiddleware = [
//...
'applyUrlLocaleToRootPage' => \Fomvasss\UrlAliases\Middleware\ApplyUrlLocaleToRootPage::class,
];
- Set in
config/url-aliases.php
: 'use_localization' => true, - Uncomment needed locales in
config/url-aliases-laravellocalization.php
and set other params - Make or change your home page (root) routes, for example:
Route::get('/{locale?}', function () {
return view('home');
})->name('home')->middleware('applyUrlLocaleToRootPage');
- 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
]);
- 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()