laravel routes not working for required parameters - php

I have one route in my routes.php like
Route::get('{subcat}', array(
'uses' => 'frontend\homeController#uploadAd'
))
And I found that the above route is effecting my userlogout route which is like this.
Route::get('userlogout', array(
'uses' => 'frontend\homeController#userlogout'
));
I am getting the {subcat} route parameter from the blade view like
subcategoryname
If I include my {subcat} route in routes.php, userlogout route does not work but, commenting out my {subcat} route or even modifying that route like:
Route::get('something/{subcat}', array(
'uses' => 'frontend\homeController#uploadAd'
))
enable user to log out from the system.
What am I doing wrong here? Is there anything that I can't specify only route parameter as my route name?

I think you can fix this by defining the logout route after the subcat route like this:
Route::get('{subcat}', array(
'uses' => 'frontend\homeController#uploadAd'
));
Route::get('userlogout', array(
'uses' => 'frontend\homeController#userlogout'
));
This is happening because the first route will match any url with a single parameter. As I know the order of defining routes is important in such cases.

Related

Laravel add dynamic parameter before route

I have a route like
http://localhost.fbleads.com/{dynamic-value}/auth/facebook-login
It give me error that route not found. Any solution for this
In your routes you could maybe have something like:
Route::get('{dynamic-value}/auth/facebook-login', [
'as' => 'auth.login',
'uses' => 'AuthController#login'
]);
the {dynamic_value} can be anything and be accessible as parameter in your controller method

get route in laravel overriding resource route , How to overcome this

I have the following route in my laravel route file:
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
The problem with the above route is , its overriding the below route:
Route::resource('admin', 'adminController');
I want to keep my resource route, but how do i keep my resource ? is there a way around this ??
Modify your route file like this.
Route::resource('admin', 'adminController');
Route::get('/{id}' , [ 'uses' => 'PagesController#show', 'as' => 'getArticle' ]);
Route files executed in the order they are defined.
If you define Route::get('/{id}',.... in the beginning and set your url like http://your-site/admin, the admin section will be considers as the id for the Route::get('/{id}',.... route. So you need to keep it in your mind when you define your route.
just move this route in the end of the web.php file.
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
There are two options:
move Route::get('/{id}', ...) after Route::resource(...)
or add a pattern to Route::get() if id is numeric Route::get('/{id}', ...)->where('id', '[0-9]+');

How to have multiple base routes in laravel 5.2

Consider the following:
Route::get('/', ['as' => 'home.index', 'uses' => 'HomeController#index']);
Route::group(['domain' => 'thechildandthepoet' . env('CONNECTION')], function() {
Route::get('/', ['as' => 'thechildandthepoet.home', 'uses' => 'GameController#index']);
});
When I go to thechildandthepoet.example.local It shows me the contents of
Route::get('/', ['as' => 'home.index', 'uses' => 'HomeController#index']);
completely by passing the fact that I told it which controller to use.
The link looks like: <li>The Child And The Poet</li>
any idea why this doesn't work?
Laravel's router executes the first route that matches given URL.
You don't specify the domain for your first route, therefore it matches all domains. The second route, even though it matches the URL as well, is ignored.
Reorganize your routes.php file, put the routes that specify the domain at the beginning and keep the most generic routes at the end.

Laravel 5 custom named routes in resource controllers

How can I pass in my own extra named routes for a resource controller?
I have:
Route::resource('logistics', 'LogisticsController', ['names' => [
'index-inbound' => 'logistics.indexInbound'
]]);
But this does not work.
You can't really add additional routes to a resource route. However, you can add any other routes you want and point them to the same controller:
Route::get('logistics/inbound', ['name' => 'logistics.index-inbound', 'uses' => 'LogistictsController#indexInbound']);
Route::resource('logistics', 'LogisticsController');
Just make sure that you register your custom routes before the resource route as otherwise they might get overridden.

Laravel 4 - route is not defined, on redirect

I'm trying to setup a simple redirect after a login.
The logging in part works but the redirect fails because it says the route doesn't exist.
This is my routes file:
Route::any('/', array('uses' => 'UsersController#login'));
Route::any('/manage', array('uses' => 'AdminController#showWelcome'));
And the route works fine if i go to http://example.com/manage .. the logo of laravel is there, and my other page is fine as well.
But when i do:
Redirect::route('/manage');
the page dies saying:
Route [/manage] not defined
Anybody have an idea?
You should use the route name when you are using Redirect::route method and in this case you have to declare the route using a name, i.e.
Route::any('/manage', array('as' => 'manage', 'uses' => 'AdminController#showWelcome'));
Here, as value is name of the route, so, now you can use
return Redirect::route('manage'); // 'manage' is the name of the route to redirect
Or, alternatively, you can use Redirect::to('url') method, i.e.
return Redirect::to('/manage'); // '/manage' is the url to redirect
Check Redirect to a named Route and named routes.
This error "Route [manage] not defined" is because of the route name "manage" is not defined.
Route name and Route path are two different things.
And you've declared the route path as admin,
Route::any('manage', 'AdminController#showWelcome');
However,
return redirect()->route('manage');
means that you are redirecting the flow to the route named "manage".
To sort the error,
Define a route name "manage" as follows in an array defined below with 'as' => 'route_name'.
Solution :
Route::any('manage', [
'as' => 'manage',
'uses' => 'AdminController#showWelcome'
]);
Please refer the link : https://laravel.com/docs/master/routing#named-routes
use return Redirect::intended('mannage');

Categories