Is it possible to make single route for multiple function? - laravel-5

Route::get('list', ['as' => '','uses' => '']);
Is it possible to make single route for multiple function?

Related

Multiple routes for the same controller function

I need to have 2 routes managed by the same controller function.
Instead of creating to different rules, I would like to create 1 rule.
So instead of something like this:
Route::get('/', ['uses' => 'MyController#index']);
Route::get('dashboard', ['uses' => 'MyController#index']);
I want to use 1 rule. I found out this multiple routes in single Route::get() call Laravel 4 but that code not works for me.
This is what I tried without success:
// THEY BOTH DO NOT WORK
Route::get('/{name}', ['uses' => 'MyController#index'])->where('name', 'dashboard|');
Route::get('/{name}', ['uses' => 'MyController#index'])->where('name', '(dashboard)?');
With these rules, if I go to / route I get 404 error.
If I go to dashboard route it works.
Making name a optional route parameter should work:
Route::get('/{name?}', ['uses' => 'MyController#index'])->where('name', 'dashboard');
Not sure why you are using uses, the shorter version would be:
Route::get('/{name?}', 'MyController#index')->where('name', 'dashboard');

What does 'as' method do in Laravel

In the example from the tutorial, it shows up.
Route::group([
'prefix' => 'admin',
'as' => 'admin.'
], function () {}
Can someone tells me what 'as' does? Also, is the dot next to the 'admin' neccessary?
Thank you.
Let's say, for example, that you have this route:
Route::get('admin', [
'as' => 'admin', 'uses' => 'AdminController#index'
]);
By using as you assign custom name to your route. So now, Laravel will allow you to reference said route by using:
$route = route('admin');
So you don't have to build the URL manually over and over again in your code. You don't really need . notation if you only want to call your route admin. If you want a more detailed name of your route, lets say for ex. admin product route, then you use the . notation, like this:
Route::get('admin/product', [
'as' => 'admin.product', 'uses' => 'AdminController#showProduct'
]);
So now, you will be able to call this route by the assigned name:
$route = route('admin.product');
Update:
The previous answer I provided is valid for a single routes. For the route groups, the procedure is very similar. In the route groups you need the . notation when you add a custom name, since you will be referencing another route after that . notation. This will allow you to set a common route name prefix for all routes within the group. So by your example, lets say you have a dashboard route inside your admin route group:
Route::group(['as' => 'admin.'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
//Some logic
}]);
});
Now, you will be able to call the dashboard route like this:
$route = route(admin.dashboard);
You can read more about this in Laravel official documentation.
you may specify an as keyword in the route group attribute array, allowing you to set a common route name prefix for all routes within the group.
For Example
Route::group(['as' => 'admin::'], function () {
// Route named "admin::"
});
UseRoute Name like {{route(admin::)}} or route('admin::')
you can use an 'as' as a named route. if you do not prefix your route name in group route than you may add custom route name like this.
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => ['2']], function () {
Route::post('/changeProfile', ['uses' => 'UserController#changeProfile',
'as' => 'changeProfile']);
});

Passing parameters to controller method from route in Laravel 5.5

I want to pass a parameter from my route to a controller method, to avoid duplication of code. For example, I could have my routes like this
Route::get('used-cars', array('uses' =>
'CarController#indexUsed'));
Route::get('new-cars', array('uses' =>
'CarController#indexNew'));
Route::get('new-and-used-cars', array('uses' =>
'CarController#indexNewAndUsed'));
and then have specific code for in each method for retrieving that car type. However, I would like to just have one index method in the controller, with a variable passed to it to indicate if it is a new or used car.
For example:
Route::get('used-cars', array('uses' =>
'CarController#index(1)'));
Route::get('new-cars', array('uses' =>
'CarController#index(2)'));
Route::get('new-and-used-cars', array('uses' =>
'CarController#index(3)'));
In previous versions of Laravel I believe this could be achieved using something like this
Route::get('/used-cars', array('as' => 'used-cars', function(){
return App::make('CarsController')->index(1);
}));
However I understand this was removed in Laravel 5.4. When I try it I only get class not found for the controller.
You could use a named parameter (type), and restrict that to specific values (new, used).
Route::get('{type}-cars', ['as' => 'cars.new-used', 'uses' => 'CarController#index'])->where('type', 'new|used');

my routing is not working proper in laravel

Here are my two routes mentioned in http/routes.php
Route::get('/{buy_type}-property/{type}-in-{city}/{location}/project/{projname}/{section}', 'APP\DetectHookController#detectProjectcase4')->where('projname', '[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('location','[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('section', '[A-Za-z0-9_\-A-Za-z0-9_\-]+');
And second one is
Route::get('/{buy_type}-property/{type}-in-{city}/{location}/project/{clustername}/{projname}', array( 'as' => 'project-with-cluster', 'uses' => 'APP\DetectHookController#detectProjectcase2'))->where('projname', '[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('location','[A-Za-z0-9_\-A-Za-z0-9_\-]+');
I want conditional routes based on the {section} parameter in first route.
The second one doesn't get call when it is supposed to be called as both routes are having same parameters. Can someone suggest me as I am helpless for almost a week.
You can re-structure your route as:
Route::get('/{buy_type}-property/{type}-in-{city}/{location}/project/{projname}/cluster/{clustername}', array( 'as' => 'project-with-cluster', 'uses' => 'APP\DetectHookController#detectProjectcase2'))->where('projname', '[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('location','[A-Za-z0-9_\-A-Za-z0-9_\-]+');

Laravel custom Route

Hi I want to use routes like this for a page
Route::get('{slug?}-{newsSlug?}-{id?}', array('as' => 'subpages', 'uses' => 'HomeController#subpages' ));
It does not work like this
Route::get('{slug?}/{newsSlug?}/{id?}', array('as' => 'subpages', 'uses' => 'HomeController#subpages' ));
This way the other pages do not work naturally.
Could you suggest me a solution about this.
Where you define a route like this
Route::get('{slug?}-{newsSlug?}-{id?}'...)
without a constant, it means it will cover most cases of routing, which is why other pages do not work.
For them to work, you need to put that code at the end of your routes file, so that other routes will have precedence.

Resources