Multiple Routes in Single Route::Get() Call Laravel 4

multiple routes in single Route::get() call Laravel 4

If I understand your question right I'd say:

Use Route Prefixing: http://laravel.com/docs/routing#route-prefixing

Or (Optional) Route Parameters: http://laravel.com/docs/routing#route-parameters

So for example:

Route::group(array('prefix' => '/'), function() { Route::get('dashboard', 'DashboardController@index'); });

OR

Route::get('/{dashboard?}', array('as' => 'dashboard', 'uses' => 'DashboardController@index'));

Laravel 8 Multiple Routes With The Same Url

I'd advise just adding a prefix to all those routes. That way you don't get any duplicate uris.


If you really don't want to do that, I've got another possibility, but you won't be able to keep the route names.

Basically, use an invokable controller to reroute to the correct controller/action.

php artisan make:controller -i
Route::group(['middleware' => ['auth']], function () {
Route::get('/home', 'App\Http\Controllers\MyInvokableController')->name('home');
Route::get('/proposal', 'App\Http\Controllers\MyInvokableController')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\MyInvokableController')->name('thesis');
});
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyInvokableController extends Controller
{
private $lookupArray = [
'home' => 'index',
'proposal' => 'proposal',
'thesis' => 'thesis',
];

public function __invoke(Request $request)
{
$role = ;// get your user's role
$urlSegment = $request->segment(count($request->segments()) - 1); // 'home', 'proposal' or 'thesis'

if ($role === 'student') {
return app(StudentController::class)->{$this->lookupArray[$urlSegment]}($request);
} elseif ($role === 'supervisor') {
return app(supervisorController::class)->{$this->lookupArray[$urlSegment]}($request);
} elseif ($role === 'hod') {
return app(hodController::class)->{$this->lookupArray[$urlSegment]}($request);
} else {
throw new \Exception("Unexpected role $role.");
}
}
}

multiple routes in single Method controller in laravel 4

Yes but you have to handle null parameters, like that:

 public function bar($id=null, $date=null)
{
....

But at this point its better to declare e single route with optional parameters:

 Route::get('foo/bar/{id?}/{date?}', 'FooController@bar');

Multiple routes for the same controller function

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');

How to group multiple routes with the same controller method?

I'm not sure why do you say that RegEx is ugly. I basically think RegEx is one of the most powerful tools.

In your case, I think the below snippet should do the job:

Route::get('user/{name}', function ($name) { // }) ->where('name', '(foo|bar|baz)');

The (foo|bar|baz) RegExr will match any of these string: 'foo', 'bar', 'baz'. So, if you need more, just add pipe (|) and add the needed string.

How can I pass multiple routes as arguments to the Route::is() method in Laravel 8?

You can do that as shown below

@if(request()->routeIs(['user','register','login']))
Do something
@endif


Related Topics



Leave a reply



Submit