Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

v0sn's avatar
Level 1

I feel like i misunderstood routing

Hello Community,

so i've been coding with laravel for a few weeks now, but i still feel like i somehow misunderstood routing.

example:

Route::view('/password', 'changePassword')->name('changePasswordView')->middleware('auth');

Route::get('/password/change', 'UserController@changePassword')->name('changePassword')->middleware('auth');

As you can see in the example above, i have a route for the change password page (view) And then i have a route for the change password action(?)

I feel like this isn't the right way to do. I do this very often, and i really haveing trouble finding names for those routes then.

Another example would

Route::view('create', 'pages/admin/users/create')->name('view.create');
Route::post('create', 'AdminController@createUser')->name('user.create');

Here i tried to give the routes better names.. but still.. i don't know if im doing this right..

i would really appreciate if some of you guys could give me some tips

Cheers

0 likes
3 replies
click's avatar

BTW, there are default routes for password reset / login / registration .

Keep the naming simple. Your controller should describe what you are viewing, your actions should describe what your are doing. Viewing 'User', Do: 'create'.

For further routing logic I would recommend the so called 'Resource Routes'. See the documentation about Resource Controllers: https://laravel.com/docs/5.6/controllers#resource-controllers

A resource is for example a 'user'. You can list all users, show one user, add one user, update one user and delete one user. The same for a 'blog post', the same for 'car', etc. etc.

A resource route is actually a wrapper that generates a view routes for you. For example:

Route::resource('users','UsersController');

// generates:
Route::get('users','UsersController@index')->name('users.index');
Route::get('users/{users}','UsersController@show')->name('users.show');
Route::get('users/create','UsersController@create')->name('users.create');
Route::post('users','UsersController@store')->name('users.store');
Route::get('users/edit','UsersController@edit')->name('users.edit');
Route::put('users/{user}','UsersController@update')->name('users.update');
Route::delete('users/{user}','UsersController@destroy')->name('users.destroy');

And your UsersController would look like:

class UsersController extends Controller
{
    public function index() 
    {
        // show all users
    }

    public function show(User $user) 
    {
        // show one user
    }

    public function create() 
    {
        // show form to create one user
    }

    public function store() 
    {
        // save one user
    }

    public function edit(User $user) 
    {
        // show form to edit one user
    }

    public function update(User $user) 
    {
        // update one user
    }

    public function destroy(User $user) 
    {
        // delete one user
    }
}

And the beauty is that it also worked with nested routes. For example if you want to see all blog posts for a user you create a route:

Route::resource('users.posts','UserPostsController');
// note the . instead of the / you would expect in a route

This will create routes:

Route::get('users/{users}/posts','UserPostsController@index')->name('users.posts.index');
Route::get('users/{users}/posts/{posts}','UserPostsController@show')->name('users.posts.show');
// etc. 

And gives the controller looks like:

class UserPostsController extends Controller
{
    public function index(User $user) 
    {
        // show all posts of user
    }

    public function show(User $user, Post $post) 
    {
        // show one post of user
    }
   // ... etc
1 like
topvillas's avatar

There isn't a right or wrong way to name your routes. Just do what you're happy with.

Seriously, stop sweating stuff like this. You'll find far bigger things to worry about.

1 like

Please or to participate in this conversation.