Ivanka Todorova
Ivanka Todorova

Reputation: 10219

How to redirect to a specific route declared as Route::controllers

I'm using Laravel 5 and I want to redirect to the login page from another controller. I'm used to using Redirect::route, but the routes are defined as Route::controllers.

The routes that come with laravel's installation:

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

Is there a way I could declare some of the routes as named route and then use it in Redirect::route?

Upvotes: 1

Views: 147

Answers (1)

MaGnetas
MaGnetas

Reputation: 5008

You can name your routes inside the controller route definition:

If you would like to name some of the routes on the controller, you may pass an array of names as the third argument to the controller method:

Route::controller('users', 'UserController', [
    'getShow' => 'user.show',
]);

Then you should be able to get the url like this:

$url = route('user.show');

Or you can get the url without assigning a name by using both controller and action:

$url = action('UserController@getShow');

Finally there's always a "direct" url:

$url = url('user/show');

Info from laravel docs

About naming directly inside controllers method. That's from laravel core:

public function controllers(array $controllers)
{
    foreach ($controllers as $uri => $controller)
    {
        $this->controller($uri, $controller);
    }
}

As you can see names are not accepted or passed to controller method so you probably can't do that. If you wanna assign route names you'll have to split Route:controllers into separate Route::controller and add names array.

Upvotes: 3

Related Questions