Named Routes Conflict Laravel 5.2 - php

I am creating a Multilingual Laravel 5.2 application and I am trying to figure out how to change language when I already have the content.
routes.php
...
Route::get('home', [
'as' => 'index',
'uses' => 'SiteController#home'
]);
Route::get([
'as' => 'index',
'uses' => 'SiteController#inicio'
]);
...
I have SiteController#home and SiteController#inicio. So I change session('language') in SiteController#change_language like:
...
public function change_language ($lang){
session(['language' => $lang]);
return redirect()->action(SAME NAMED ROUTE, DIFFERENT LANGUAGE);
}
...
So, When I click on a button with
English
from /inicio (SiteController#inicio) I should be redirected to the same named route (SiteController#home) so I can check the language and display appropriate content.
Any ideas of how to get the named route or something helpful?
Thank you :)

Related

Admin route group with prefix, middleware and named routes

I have some named routes in a controller named VehicleController:
vehicle.index
vehicle.show
And then I have an admin section, where I have defined a route group with prefix and middleware. In this section I have a resource controller name AdminVehicleController to handle CRUD tasks for the Vehicle (not sure if this is best practice) with the following routes:
vehicle.index
vehicle.create
vehicle.store
...
However these named routes are conflicting. My routes web.php looks like this for now:
Route::get('vehicles', 'VehicleController#index')->name('vehicle.index');
Route::get('vehicle/{vehicle}', 'VehicleController#show')->name('vehicle.show');
Route::group(['prefix' => 'admin', 'middleware' => 'is.admin'], function () {
Route::get('/', 'AdminDashboardController#index');
Route::resource('vehicle', 'AdminVehicleController');
});
If I add 'name' => 'admin' to the Route::group() array, the route names will be adminvehicle.index and not admin.vehicle.index.
What is the correct way to combine all these parameters in the route?
Try to use as parameter for your admin group
Route::group(['prefix' => 'admin', 'middleware' => 'is.admin', 'as'=> 'admin.'], function () {
Route::get('/', 'AdminDashboardController#index')->name('dashboard');
Route::resource('vehicle', 'AdminVehicleController');
});
Reference Link
Supply a names array as part of the third parameter $options array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.
Route::resource('vehicle', 'AdminVehicleController', [
'names' => [
'index' => 'admin.vehicle.index',
// etc...
]
]);

Groups in group on routing Laravel

I really need help!
On the Laravel 5.3, it seems that a group of routes into an another group isn't working for me. Is there any solution to make it work ? Or do I have to write my routes in another way ? This is the way I write my routes:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]
],
function()
{
Route::group([
'prefix' => 'admin'
], function () {
// General Settings
Route::get('settings', [
'as' => 'admin.settings',
'uses' => 'AdminController#showSettings'
]);
});
});
I am using a prefix in the first group for the multiple language and the another group is for the admin section. I want an url like this : /en/admin/settings
The problem is solved! It did not work because I had used another route in which there was an paramater before the group of admin section ex:
Route::get('activate/{code}', [
'as' => 'auth.activation',
'uses' => 'RegistrationController#getActivate'
]);

Laravel: To pass common parameter value for all route in laravel

** I have Multiple Route in Laravel. I wanna pass common parameter for every Route i,e If i passed POST as a parameter in any route, I need to Call POST controller for all URL.
My Routes are below:
Original URL: www.mydomain.com/home/
Required URL: www.mydomain.com/home/post
Original URL: www.mydomain.com/follow/
Required URL: www.mydomain.com/follow/post
In above URL I have two separate blades like home.blade.php AND post.blade.php,
In Second Example I have two separate blades like follow.blade.php AND post.blade.php. Here Post.blade.php is common.
For Both home and follow must call postcontroller. My Route Controllers . **
//for follow Route
Route::get('Follow', [
'as' => 'Follow',
'uses' => 'PageController#getFollow'
]);
//for Home Route
Route::get('Home', [
'as' => 'Home',
'uses' => 'PageController#getHome'
]);
Post Route controller is below
Route::get('Post', [
'as' => 'Post',
'uses' => 'PostController#getPOST'
]);

How to differentiate parameter vs declared route when routing?

Having the next routes:
Route::get('/apartment/{apartment_name}', 'ApartmentController#getApartmentByName');
Route::get('/apartment/create', [
'uses' => 'ApartmentController#create',
'as' => 'apartment.create'
]);
Route::get('/apartment/edit', [
'uses' => 'ApartmentController#edit',
'as' => 'apartment.edit',
]);
How could I make a difference between the routes
myapp.com/apartment/create and myapp.com/apartment/beach-apartment
I would like to search by the apartment's name with the same URI prefix (apartment/) but with this code I'm always calling the parameter route.
It is because whatever is being called, create or edit, is being matched within the parameter one, /apartment/{apartment_name}, as create or edit equals to the apartment_name.
Just move the parameter one to the lower most line within that block.
Route::get('/apartment/create', [
'uses' => 'ApartmentController#create',
'as' => 'apartment.create'
]);
Route::get('/apartment/edit', [
'uses' => 'ApartmentController#edit',
'as' => 'apartment.edit',
]);
Route::get('/apartment/{apartment_name}', 'ApartmentController#getApartmentByName');
With this configuration, if the /apartment/create or /apartment/edit is not matched, then it will match /apartment/{apartment_name}.

Laravel - Get route name in filter

How can I get current route name in filter? I tried use Route::currentRouteName(); but it's null.
Route::filter('belongsToUser', function(){
dd( Route::currentRouteName() );
exit;
});
Route looks for example:
Route::get('/openTicket/{id}', array('before' => 'auth|belongsToUser', 'uses' => 'MyController#MyAction'));
Your route isn't named, so it's no surprise the route name is null. You need an as parameter.
Route::get('/openTicket/{id}', array(
'as' => 'yourRouteName',
'before' => 'auth|belongsToUser',
'uses' => 'MyController#MyAction'));
http://laravel.com/docs/routing#named-routes

Categories