I have an instance where I would like multiple variations to resolve to one view. Is there a way of doing this without repeating the function within routes?
Example:
Route::group(['prefix' => 'tools'], function () {
Route::any('', function () {
return View::make("tools.profile");
});
Route::any('/', function () {
return View::make("tools.profile");
});
Route::any('view', function () {
return View::make("tools.profile");
});
Route::any('view/{id}', 'Tools@profileUpdate', function ($id) {});
});
In the above example I would like ''
, /
and view
to resolve to view View::make("tools.profile")
.
Is there something like below an option, where an array can be parsed?
Route::group(['prefix' => 'tools'], function () {
Route::any(['','/','view'], function () {
return View::make("tools.profile");
});
Route::any('view/{id}', 'Tools@profileUpdate', function ($id) {});
});
i think this will help you
Route::group(['prefix' => 'tools'], function () {
Route::get('/{name}',function(){
return View::make("tools.profile");
})->where('name', '|/|view');
Route::any('view/{id}', 'Tools@profileUpdate', function ($id) {});
});
yes u can do by array
$routes = array('','/','view');
foreach ($routes as $route) {
Route::get($route, function () {
return view('tools.profile');
});
}
if you want pass route(viewname)
then do like this by using closures
$routes = array('','/','view');
foreach ($routes as $route) {
Route::get($route, function () use ($route) {
info("view name ".$route);
return view('tools.profile');
});
}
Yes, you must simply invest what you are doing:
Route::group(['prefix' => 'tools'], function () {
Route::get('view/{id}', 'Tools@profileUpdate');
Route::get('{x}', function ($x) {
return view("tools.profile");
});
});
And if you want more security:
Route::group(['prefix' => 'tools'], function () {
Route::get('view/{id}', 'Tools@profileUpdate');
Route::get('{x}', function ($x) {
if($x == '' || $x == '/' || $x == 'view'){
return view("tools.profile");
}else{
/*Redirect to some where or make a error view or someting*/
}
});
});