Variables in route group prefixes in Laravel 4 - php

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']) }}

Related

Best way to write Group routes with prefix in laravel 5.5

What is the best and correct way to write the following routing:
Route::group(['middleware' => ['web']],function (){
Route::prefix('user')->group(function () {
//this address shows the login page
Route::any('login', 'User#login_page');
//others address that control the login action
Route::prefix('login')->group(function (){
Route::get('google', 'User#check_user_login_with_google');
Route::post('form', 'User#check_user_login_with_form');
Route::get('google-url', 'User#redirect_to_google_url');
});
//these address control the registration actions
Route::any('register','User#register');
Route::any('register/check','User#check_user_registration');
});
});
You can do it all in one shot:
Route::group(['prefix' => 'user', 'as' => 'user.', 'middleware' => ['web']], function() {
Route::any('login', 'User#login_page');
...
})
You can use all these in a separate associated array and assign this array to the group here is how to do that .
Route::group(['prefix' => 'user', 'as' => 'user.', 'middleware' => ['web']], function() {
Route::any('login', 'User#login_page');
...
})
Hope this may help you

Laravel 4 how get routes by group name

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

How to use Redirect::intended in laravel?

I want the route to bypass authentication filter after successfully login. So I use Redirect::intended. But it makes problem. Here is my code
In LoginController#doLogin
if (Auth::attempt($credentials)) {
return Redirect::intended("home.index");
}
In Routes
Route::group(array('before' => 'auth'), function() {
Route::resource('home', 'HomeController');
Route::get('/', 'HomeController');
});
My routes
If I put the the home resource route without the auth filter, then it will work. (Not redirect to login route.).That code is given below
Route::resource('home', 'HomeController');
Route::group(array('before' => 'auth'), function() {
Route::get('/', 'HomeController');
}); /* No Problem with this code */
But I want to work with auth filter.I'm using laravel 4.
Please help me...
Currently I use this:
in filter.php
Route::filter('sentry', function() {
if (!Sentry::check())
{
return Redirect::route('authLogin');
}
});
so in route.php
Route::get('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::post('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::group(array('before' => 'sentry'), function() {
Route::get('user/', array('as' => 'user', 'uses' => 'UserController#index'));
Route::get('user/index', array('as' => 'userIndex', 'uses' => 'UserController#index'));
});
I preffer named routes.

Laravel 4: Add a filter to a route a pass it a controller

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.

laravel route on logged in not triggering

I'm trying to route the index page to a different location if logged in however even though my authentication system works, it's not redirecting to where I expected i.e. getLogged, instead it always redirects to getIndex whether I am logged in or not.
Route::filter('auth', function()
{
if (!Sentry::check()) return Redirect::to('/');
});
Route::group(array('before' => 'auth'), function() {
Route::get('/', array('uses' => 'MyController#getLogged'));
});
Route::get('/', array('before' => 'detectLang', 'uses' => 'MyController#getIndex'));
I tested to make sure my auth works by changing
Route::group(array('before' => 'auth'), function() {
Route::get('/', array('uses' => 'MyController#getLogged'));
});
to
Route::group(array('before' => 'auth'), function() {
Route::get('/dash', array('uses' => 'MyController#getLogged'));
});
and that properly behaves that I can only access /dash when I am logged in so why is my index route not working?
You're declaring the same route twice, it won't work. To achieve this functionality, instead of adding a auth filter, add a guest one that, instead of checking if the user is not connected, will check if it is. Something like this:
Route::filter('guest', function () {
if (Sentry::check()) return Redirect::route('logged');
});
Then, setup your routes, something along these lines:
Route::get('/', array(
'as' => 'home',
'uses' => 'MyController#getIndex',
'before' => 'guest'
));
Route::get('/logged', array(
'as' => 'logged',
'uses' => 'MyController#getLogged',
'before' => 'auth|detectLang'
));
Note: The as key gives a name to your route, so you can use it on Redirect::route or URL::route methods.

Categories