Reputation:
What I'm trying to accomplish is, I have a route which accepts a parameter named type
and It accepts any value now. But I want to provide choices to accept value from, If matches the route works, otherwise It throws Not found error.
Choices are:
Code:
Route::get('/activity/{type}/status','ActivitiesController@status');
ActivitiesController.php
class ActivitiesController extends Controller
{
public function status($type, Request $request)
{
return $request->all();
}
}
Upvotes: 4
Views: 1575
Reputation: 9998
You can use regular expression constraints like:
Route::get('/activity/{type}/status','ActivitiesController@status')
->where('type', '(retweet|favorite|tweet)');
Upvotes: 7
Reputation: 163858
This will check the $type
. If it's not appropriate one, it will redirect to 404 error page:
public function status($type, Request $request)
{
if ($type != 'retweet' && $type != 'favorite' && $type != 'tweet') {
abort(404);
}
return $request->all();
}
Alternative:
Route::get('/activity/{type}/status', function()
{
if ($type == 'retweet' || $type == 'favorite' || $type == 'tweet') {
$app = app();
$controller = $app->make('ActivitiesController');
$controller->callAction($app, $app['router'], 'status', $parameters = array());
}
});
Alternative 2:
Only if you have just 3 types and you will not add any in future. This is stupid one, but more readable and easier to maintain:
Route::get('/activity/retweet/status','ActivitiesController@status');
Route::get('/activity/favorite/status','ActivitiesController@status');
Route::get('/activity/tweet/status','ActivitiesController@status');
Also, you could use middleware. The choice is yours. )
Upvotes: 1