In Laravel, I know I can get all routes using `Route::getRoutes() but I can't find if it is possible to get a list of all routes contained in a specified group.
For example, i have this route file:
Route::group(array('group_name' => 'pages'), function() {
Route::any('/authentication', array('as' => 'authentication', 'uses' => 'LogController#authForm' ));
Route::group(array('before' => 'auth_administration'), function() {
Route::any('/tags_category/index', array('as' => 'index-tags-categories', 'uses' => 'TagsCategoryController#index'));
Route::any('/tags_category/update', array('as' => 'update-tags-category', 'uses' => 'TagsCategoryController#update'));
});
});
Route::group(array('before' => 'auth_administration'), function() {
Route::any('/tags_category/store', array('as' => 'store-tags-category', 'uses' => 'TagsCategoryController#store'));
Route::any('/tags_category/update/{id}', array('as' => 'update-form-tags-category', 'uses' => 'TagsCategoryController#updateForm'));
Route::any('/tags_category/delete/{id}', array('as' => 'delete-tags-category', 'uses' => 'TagsCategoryController#delete'));
}); // operazioni protette
and in my controller i want obtain only routes contained in the first group (the one with the variable 'group_name').
Is it possible? If yes how I can do it? Thanks
The attributes passed to the group in the first parameter are stored on the route in the action array. This array can be accessed via the getAction() method on the route. So, once you get access to the route objects, you can filter based on this information.
$name = 'pages';
$routeCollection = Route::getRoutes(); // RouteCollection object
$routes = $routeCollection->getRoutes(); // array of route objects
$grouped_routes = array_filter($routes, function($route) use ($name) {
$action = $route->getAction();
if (isset($action['group_name'])) {
// for the first level groups, $action['group_name'] will be a string
// for nested groups, $action['group_name'] will be an array
if (is_array($action['group_name'])) {
return in_array($name, $action['group_name']);
} else {
return $action['group_name'] == $name;
}
}
return false;
});
// array containing the route objects in the 'pages' group
dd($grouped_routes);
User artisan to list all routes for the application
php artisan routes --name=admin
in Laravel 5
php artisan route:list --name=admin
Related
I want to create dynamic route name for my app. Here is my route file
Route::group(['prefix' => '{team}/dashboard', 'middleware' => 'isMember'], function() {
Route::get('/user', array('uses' => 'UserController#index', 'as' => 'user.index'));
Route::get('/user/edit/{id}', array('uses' => 'UserController#edit', 'as' => 'user.edit'));
Route::patch('/user/{id}', array('uses' => 'UserController#update', 'as' => 'user.update'));
Route::delete('/user/{id}', array('uses' => 'UserController#destroy', 'as' => 'user.delete'));
it's not simple if i have to define route like this
'route' => ['user.delete', $team, $user->id]
or
public function destroy($team,$id) {
// do something
return redirect()->route('user.index', $team);
}
I want to generate route name like "$myteam.user.delete" or something more simplier like when i define "user.delete" it includes my team name.
How i can do that? is it possible?
You could do that by setting as. Also using resource routes will be handy.
$routeName = 'team.';
Route::group(['as' => $routeName], function(){
Route::resource('user', 'UserController');
});
Now you can call like
route('team.user.index');
More on resource routes here https://laravel.com/docs/5.3/controllers#resource-controllers
try this:
Route::delete('/user/{team}/{id}', array('uses' => 'UserController#deleteTeamMember', 'as' => 'myteam.user.delete'));
Now call the route as:
route('myteam.user.delete', [$team, $id]);
I have following routes:
Route::group(['prefix' => 'group1'], function () {
Route::get('view1', ['as' => 'group1_view1', 'uses' => 'group1Controller#get_view1']);
Route::get('view2', ['as' => 'group1_view2', 'uses' => 'group1Controller#get_view2']);
});
Route::group(['prefix' => 'group2'], function () {
Route::get('view1', ['as' => 'group2_view1', 'uses' => 'group2Controller#get_view1']);
Route::get('view2', ['as' => 'group2_view2', 'uses' => 'group2Controller#get_view2']);
});
I wish to pass variables, for example, $title = 'group-one' to all views in group1 and $title = 'group-two' to all views in group2. Instead of adding variable $title in all methods in each group controller, is it possible to pass variables groupwise in routes?
NO you cannot pass variables into route groups, but if in case you need one piece of view/code in multiple views, title for example, you can use view composers instead.
Suppose I have multiple routes in my Laravel routes.php Like-
Route::post( 'createClan',array('uses'=>'MyClan#createClan'));
Route::post( 'joinClan',array('uses'=>'MyClan#joinClan'));
Route::get( 'myclan', array( 'as' => 'myclan' , 'uses' => 'MyClan#myclan' ));
Route::get( 'clanDetails', array( 'as' => 'clanDetails' , 'uses' => 'MyClan#clanDetails' ));
Route::get( 'myclanDefault', array('as' => 'myclanDefault' , 'uses' => 'MyClan#myclanDefault' ));
And I have Controller named 'Common' and I have a 'ValidateRoute' function inside that common like this-
public function ValidateRoute($UserId, $form_name){
switch ($form_name)
{
case 'createJoin'://Create/Join Clan Menu Option.
$createJoin=TRUE;
$clans= Userclanmapping::where('user_id','=',$UserId)->where('active','=',1)->get(array('clan_id'));
foreach($clans as $clan)
{
$createJoin=FALSE;
}
return $createJoin;
}
}
My problem is that I want to call that function before calling that route. And If the function returns true only then I can open that route but if function returns false I should be moved to another route or I should get a message like route not found.
In short, I am trying to apply a custom check on every route. A route is only accessible only if it fulfills that conditions.
I tried this code-
Route::get( 'myclanDefault', array( 'before'=>'common:ValidateMenuOptions(Session::get(\'userid\'),\'createJoin\'))','as' => 'myclanDefault' , 'uses' => 'MyClan#myclanDefault' ));
But this didn't work.
You can use route filters per route basis using something like this:
// In filters.php
Route::filter('myclanDefaultFilter', function($route, $request){
// ...
if(someCondition) {
return Redirect::route('routeName');
}
});
Then declare the route like this:
Route::get(
'myclanDefault',
array(
'before' => 'myclanDefaultFilter',
'as' => 'myclanDefault',
'uses' => 'MyClan#myclanDefault'
)
);
Now before myclanDefault is dispatched to the action the myclanDefaultFilter filter will run first and you may redirect from that filter depending a certain condition.
Also, you may declare a global before event for all the routes using something like this:
App::before(function($request){
// This event will fire for every route
// before they take any further action
});
How to you add a filter to a route and pass a controller to it?.
In Laravel's doc they said that you can add a filter to a route like this:
Route::get('/', array('before' => 'auth', function()
{
return 'Not Authorized';
}));
But I need to pass a controller, like this:
Route::get('/', array('before' => 'auth', 'HomeController#index'));
But I get this error when I do it like that:
call_user_func_array() expects parameter 1 to be a valid callback, no array or string given
Any idea?
You should pass the controller function with uses key, So replace,
Route::get('/', array('before' => 'auth', 'HomeController#index'));
With,
Route::get('/', array('as' => 'home', 'before' => 'auth', 'uses' => 'HomeController#index'));
And there should be a route for login to process the auth filter like this.
Route::get('login', function()
{
if(Auth::user()) {
return Redirect::to('/');
}
return View::make('login');
});
Wanted to add another solution to your problem.
You can also use this, which in my opinion feels more readable.
Route::get('/', 'HomeController#index')->before('auth');
You only need to use "as" and "uses" if you're in need of named routes, eg. for a Form route.
Given the below routes it will respond to
http://example.com/game/stats/123
http://example.com/game/stats/game/123
http://example.com/game/stats/reviewer/123
What I want to know is, how can I make it respond to
http://example.com/game/123/stats
http://example.com/game/123/stats/game
http://example.com/game/123/stats/reviewer
I tried doing
Route::group(['prefix' => 'game/{game}'], function($game){
But that fails with "Missing argument 1 for {closure}()"
Note that there are four other groups apart from stats but I have omitted them for this example for brevity.
Route::group(['prefix' => 'game'], function(){
Route::group(['prefix' => 'stats'], function(){
Route::get('/{game}', ['as' => 'game.stats', function ($game) {
return View::make('competitions.game.allstats');
}]);
Route::get('game/{game}', ['as' => 'game.stats.game', function ($game) {
return View::make('competitions.game.gamestats');
}]);
Route::get('reviewer/{game}', ['as' => 'game.stats.reviewer', function ($game) {
return View::make('competitions.game.reviewstats');
}]);
});
});
Can you try this code see if it's what you want. Here the second group route it's just the {gameId}and then you have the stats group which wraps all the other routes.
Route::group(['prefix' => 'game'], function(){
Route::group(['prefix' => '{gameId}'], function(){
Route::group(['prefix' => 'stats'], function(){
Route::get('/', ['as' => 'game.stats', function ($game) {
return View::make('competitions.game.allstats');
}]);
Route::get('game', ['as' => 'game.stats.game', function ($game) {
return View::make('competitions.game.gamestats');
}]);
Route::get('reviewer', ['as' => 'game.stats.reviewer', function ($game) {
return View::make('competitions.game.reviewstats');
}]);
});
});
});
And then in your views you can call them by the route name and pass the gameId to the route;
{{ link_to_route('game.stats','All Stats',123) }} // game/123/stats/
{{ link_to_route('game.stats.game','Game Stats',123) }} // game/123/stats/game
{{ link_to_route('game.stats.reviewer','Review Stats',123) }} // game/123/stats/reviewer
Hope this helps and solves your problem.
EDIT
I just checked It should work also with Route::group(['prefix' => 'game/{game}' as you have tried but just make sure to pass the game argument when creating the route like stated above. If you have more variables to pass you can pass an array to the function.
{{ link_to_route('game.stats','All Stats',['game' => '123','someOtherVar' => '456']) }}