PHP code example of fomvasss / laravel-url-aliases
1. Go to this page and download the library: Download fomvasss/laravel-url-aliases library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fomvasss / laravel-url-aliases example snippets
protected $middleware = [
//...
\Fomvasss\UrlAliases\Middleware\UrlAliasMiddleware::class,
];
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');
//...
});
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'));
}
protected $routeMiddleware = [
//...
'applyUrlLocaleToRootPage' => \Fomvasss\UrlAliases\Middleware\ApplyUrlLocaleToRootPage::class,
];
Route::get('/{locale?}', function () {
return view('home');
})->name('home')->middleware('applyUrlLocaleToRootPage');
$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
]);
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()
shell
php artisan vendor:publish --provider="Fomvasss\UrlAliases\ServiceProvider"
shell
php artisan migrate