Use custom function in Controller when using resource in routes - Laravel

10,743

Using Resource will generate following route names by default

user.index
user.create
user.store
user.show
user.edit
user.update
user.destroy

more information RESTful Resource Controllers

If you want to create another route you could do this way

Route::get('user/login', 'UserController@login');

Route::resource('user', 'UserController');

note: you should define those routes before your call to Route::resource

for more information you can look at RESTful Resource Controllers in Adding Additional Routes To Resource Controllers session

Share:
10,743
Lovelock
Author by

Lovelock

Updated on June 15, 2022

Comments

  • Lovelock
    Lovelock almost 2 years

    Been building a framework website using Laravel and working on the user system.

    I am using the controller by a resource route:

    Route::resource('user', 'UserController');
    

    Which works fine for all the normal create, index, store etc function in the controller.

    For my registration form this is the opening:

    {{ Form::open(array('route' => 'user.store', 'class'=>'small-form form-holder')) }}
    

    Thinking how nice this is, I created a login function in my UserController and tried this for my login form:

    {{ Form::open(array('route' => 'user.login', 'class'=>'small-form form-holder')) }}
    

    However this returns a route not defined error. Is this because of the resource route that I set? I know I could set a custom route which uses the controllers login method but I like this way of doing things.