Navtools package
1 - From the command line run, (*2)
composer require syscover/pulsar-navtools
2 - To publish package, you must type on console, (*3)
php artisan vendor:publish --provider="Syscover\Navtools\NavtoolsServiceProvider"
3 - Register middlewares pulsar.navtools on file app/Http/Kernel.php add to routeMiddleware array, (*4)
'pulsar.navtools' => \Syscover\Navtools\Middleware\Navtools::class,
Set url type for you web, you have three types, lang, country or lang-country, for urls type lang:, (*5)
NAVTOOLS_URL_TYPE=lang
you can work with this urls, (*6)
hrrp://mydomain.com/en/any-page
for urls type country, (*7)
NAVTOOLS_URL_TYPE=country
you can work with this urls, (*8)
hrrp://mydomain.com/us/any-page
for urls type lang-country, (*9)
NAVTOOLS_URL_TYPE=lang-country
you can work with this urls, (*10)
hrrp://mydomain.com/en-us/any-page
Set available languages in your web, (*11)
NAVTOOLS_LANGS=en|es
Set available countries in your web, (*12)
NAVTOOLS_COUNTRIES=us|gb|es
Set default country for your web, (*13)
NAVTOOLS_DEFAULT_COUNTRY=es
On app\Http\routes.php file use this closure to implement routes with translation, (*14)
Route::group(['middleware' => ['pulsar.navtools']], function() { // write here your routes });
You have several url configuration options depends on the chosen NAVTOOLS_URL_TYPE parameter:, (*15)
Write your routes with lang variable, (*16)
Route::group(['middleware' => ['pulsar.navtools']], function() { Route::get('/', function(){ return view('www.index'); }); Route::get('{lang}', function(){ return view('www.index'); }); Route::post('{lang}/contact', ['as'=>'contact', 'uses'=>'FrontEndController@contact']); });
Or set lang variable on your routes, (*17)
Route::group(['middleware' => ['pulsar.navtools']], function() { Route::get('/', function(){ return view('www.index'); }); Route::get('en', function(){ return view('www.index'); }); Route::get('es', function(){ return view('www.index'); }); Route::post('en/contact', ['as' => 'contact-en', 'uses'=>'FrontEndController@contact']); Route::post('es/contacto', ['as' => 'contact-es', 'uses'=>'FrontEndController@contact']); });
Or set constant lang but country variable, (*18)
Route::group(['middleware' => ['pulsar.navtools']], function() { Route::get('/', function(){ return view('www.index'); }); Route::get('/en-{country}', function(){ return view('www.index'); }); Route::get('/es-{country}', function(){ return view('www.index'); }); Route::post('en-{country}'/contact', ['as' => 'contact-en', 'uses'=>'FrontEndController@contact']); Route::post('es-{country}'/contacto', ['as' => 'contact-es', 'uses'=>'FrontEndController@contact']); });
Or use lang and country variables to get language value., (*19)
Route::group(['middleware' => ['pulsar.navtools']], function() { Route::get('/', function(){ return view('www.index'); }); Route::get('/{lang}-{country}', function(){ return view('www.index'); }); Route::post('/{lang}-{country}/contact', ['as' => 'contact-en', 'uses'=>'FrontEndController@contact']); });
Get country from user, a simple case, (*20)
user_country(); // get country user
Get lang from user, a simple case, (*21)
user_lang(); // get language user
To set routes you need to add lang or country parameters depending on NAVTOOLS_URL_TYPE., (*22)
route('routeName', ['lang' => 'en', 'country' => 'us']);
You can use a custom helper nt_route(), this helper inserts automatically variables lang and country, unless you specify these variables., (*23)
nt_route('routeName');
You can use redirect() helper without any trouble, we have extended Laravel core so that redirect()->route() does the same as nt_route()., (*24)
You can change the language with change_lang helper, (*25)
change_lang('en');
You can change the country with change_country helper, (*26)
change_country('us');
Active route is a helper for to know when any route is active, has this properties: * route:string|array = Route name or array of route names to target * calls:string default:null = If isset this variable, helper will return the indicated string or a boolean * firstOccurrence:float = Check that the first section of the url matches the route, (*27)
A simple case, (*28)
<a class="{{ active_route('home', 'active') }}" href="{{ route('home') }}">HOME</a>
case with multiples routes, (*29)
<a class="{{ active_route(['home', 'home-en', 'home-es'], 'active') }}" href="{{ route('home') }}">HOME</a>
case with nested route, (*30)
<a class="{{ active_route('product', 'active', true) }}" href="{{ route('product-01') }}">PRODUCT</a>
Get the route name of current route in other language, (*31)
<a href="{{ route(get_lang_route_name('en')) }}">Change Lang</a>
Get the url of current route in other language, (*32)
<a href="{{ get_lang_route('en') }}">Change Lang</a>
The Navtools is open-sourced software licensed under the MIT license., (*33)