Two routes in laravel getting confused, I need a solution to this - laravel-5

1. Route::get('/admin', [
'uses' => 'AdminController#admin',
'as' => 'admin' ]);
2. Route::get('/{uri}', [
'uses' => 'ArticleController#article',
'as' => 'article' ]);
if I try to go to admin route it goes to article route, I need a solution for both routes to work.

The conflict arrives in your case due to /{uri} matches to /admin ,
For example. /xyz, /abc and even /admin match to /{uri} route.
You can do something like this to avoid it.
Route::get('/admin', [ 'uses' => 'AdminController#admin', 'as' => 'admin' ]);
Route::get('/uri/{uri}', [ 'uses' => 'ArticleController#article', 'as' => 'article' ]);
Note that now /admin will match to your /admin route and /uri/{parameter} will match to your /uri/{uri} route

You have to write route 1 first then route 2
Follow: Laracasts

Related

I have problem with slug and id in web route

i have this code
Route::get('posts/{id}/postedit','PostController#postedit');
Route::post('posts/{id}/postupdate','PostController#postupdate');
Route::get('posts/{id}/postdelete','PostController#postdelete');
Route::get('/{slug}', [
'uses' => 'SiteController#singlepost',
'as' => 'site.single.post'
]);
Route::get('/{id}', [
'uses' => 'PendaftaranAlumniController#test',
'as' => 'test',
]);
so here is the problem, when i run like this it Route::get('/{slug}' it works fine but Route::get('/{id}' it doesnt work
otherwise when i put Route::get('/{id} in the top it works fine, but Route::get('/{slug}' doesn`t work anymore.
how can i solve this?
Because both /{slug} and /{id} is getting treated as same by Laravel, both the routes /{slug} and /{id} will point any route defined earlier.
You can use it as it is, you need to tell Laravel that these two routes are different in any manner so that Laravel will treat it differently.
Route::get('singlepost/{slug}', [
'uses' => 'SiteController#singlepost',
'as' => 'site.single.post'
]);
Route::get('test/{id}', [
'uses' => 'PendaftaranAlumniController#test',
'as' => 'test',
]);

Laravel 5.2 validation redirect

Is it possible to redirect the user to a POST route in Laravel if validation fails?
Route::get('/order/confirm', ['middleware' => 'auth', 'uses' => 'PageController#orderConfirm']);
Route::post('/order/create', ['middleware' => 'auth', 'uses' => 'PageController#orderCreate']);
Route::post('/order/store', ['middleware' => 'auth', 'uses' => 'PageController#orderStore']);
I have 2 forms. First is confirm the order. Second is form containing the order.
If I give incorrect data in the order, shows an error message.
MethodNotAllowedHttpException in RouteCollection.php line 219:
PS: I'm sorry for my english.
Could you change
Route::post('/order/create', ['middleware' => 'auth', 'uses' => 'PageController#orderCreate']);
to
Route::get('/order/create', ['middleware' => 'auth', 'uses' => 'PageController#orderCreate']);
it will work.

Behaviour of routing in laravel

After trying out this acl tutorial I came across something I can't understand.
In laravel I created my route according to the tutorial and changed it to match laravel's auth controller (as I already used that before by installing it with bestmomo) to:
$router->get('/', [
'uses' => 'Auth\AuthController#getLogin',
'as' => 'admin.user.login',
'middleware' => ['acl:login']
]);
Like this it doesn't find my route giving me the error
NotFoundHttpException in RouteCollection.php line 161:
If I add the following route:
Route::get('/login', [
'as' => 'login', 'uses' => 'Auth\AuthController#getRegister'
]);
It works well.
Why do I need to add the second route?
Why can't the first one stand alone?
I think the blog you are following has an error.
The correct way to call a route you need is:
Route::get('/', [
'uses' => 'Auth\AuthController#getLogin',
'as' => 'admin.user.login',
'middleware' => ['acl:login']
]);
Note: you can also use a helper method and just call:
get('/', [
'uses' => 'Auth\AuthController#getLogin',
'as' => 'admin.user.login',
'middleware' => ['acl:login']
]);;
The only way I can see the the code you mentioned working is if the following is at the top of the routes file:
$router = app('router');

Laravel 5 advance routing with reserved route keywords

Would like to check say that I have the following routes
Route::group(['middleware' => 'auth'], function(){
Route::get('/{profile_url?}', array('as' => 'profile', 'uses' => 'ProfileController#getProfile'));
Route::get('/settings/password', array('as' => 'chgPassword', 'uses' => 'ProfileController#updatePassword'));
Route::post('/settings/password', array('as' => 'postChgPassword', 'uses' => 'ProfileController#postUpdatePassword'));
Route::get('/settings/email/request', array('as' => 'chgEmailRequest', 'uses' => 'ProfileController#updateEmailRequest'));
Route::post('/settings/email/request', array('as' => 'postChgEmailRequest', 'uses' => 'ProfileController#postUpdateEmailRequest'));
Route::get('/logout', array('as' => 'logout', 'uses' => 'ProfileController#logout'));
});
Notice that my first route accepts an optional parameter which will then route the user to a specific profile which it works fine, but when ever i have other routes say that /logout, laravel router will also use the /{profile_url?} route instead of the expected logout route. Is there any way that i can specified something like a reserved keyword like
Route::get('/{profile_url?}', array('as' => 'profile', 'uses' => 'ProfileController#getProfile')
->except('settings', 'logout'));
something like that? Ho that someone can enlighten me with this issue.
Because you put a wildcard {profile_url?} at the first place, Laravel will ignore the rest. So be careful when using wildcard routes. you should put the least specific route in the last place, Lavarel will check all of specific routes. If it doesn't match, it will go to the wildcard route. For example :
Route::group(['middleware' => 'auth'], function(){
Route::get('/{profile_url?}',...); // Lavarel do this
Route::get('/logout',...); // ignore this
});
Route::group(['middleware' => 'auth'], function(){
Route::get('/logout',...); // do this if it matches
Route::get('/{profile_url?}',...); // else do this
});

two different controllers with same route name

Assume I have two controllers named Client\DashboardController and Admin\DashboardController and routes.php contains the following:
Route::get('admin/', [
'uses' => 'Admin\DashboardController',
'as' => 'dashboard'
]);
Route::get('client/', [
'uses' => 'Client\DashboardController',
'as' => 'dashboard'
]);
both controllers are in different namespace but share one view. In my view I would like to do the following:
home
and depending on the url prefix it should maps to the correct controller, but the issue is it always maps to Client controller.
That's like wanting to have two function with the same name just for the sake of it and complain about the compiler for not doing what you want to.
Just namespace your routes, it'll make you life (debugging and maintaining) easier:
Route::get('admin/', [
'uses' => 'Admin\DashboardController',
'as' => 'admin.dashboard'
]);
Route::get('client/', [
'uses' => 'Client\DashboardController',
'as' => 'client.dashboard'
]);
And then:
home
home

Resources