Question

This might be a simple problem but I'm on testing with Laravel. I set my routes like this accordingly:

// Users Route
Route::get('users',array('as'=> 'users', 'uses'=> 'UsersController@index'));
Route::get('users/{id}', array('as' => 'user', 'uses' => 'UsersController@show') );
Route::get('users/{id}/edit', array('as' => 'edit_user', 'uses' => 'UsersController@edit') );
Route::get('users/new', array('as' => 'new_user', 'uses' => 'UsersController@create'));
Route::post('users',  'UsersController@create');
Route::delete('users', 'UsersController@destroy');

Now, in my browser, if I visit localhost/users/new, it will call the route named "user" not the "new_user". What I mean is, it will load the route for edit not for creating users.

What's wrong with my code?

Was it helpful?

Solution 2

Precedence matters, just change to:

Route::get('users',array('as'=> 'users', 'uses'=> 'UsersController@index'));
Route::get('users/new', array('as' => 'new_user', 'uses' => 'UsersController@create'));
Route::get('users/{id}', array('as' => 'user', 'uses' => 'UsersController@show') );
Route::get('users/{id}/edit', array('as' => 'edit_user', 'uses' => 'UsersController@edit') );
Route::post('users',  'UsersController@create');
Route::delete('users', 'UsersController@destroy');

Laravel assumed that 'new' was your {id} parameter.

OTHER TIPS

If you are using RESTful API, using the resource routing is best.

The route,

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

And the controller,

<?php


class UsersController extends BaseController {

    /**
     * Display all users.
     *
     * @return Response
     * GET http://localhost/laravel/users
     */

    public function index() {

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */

    public function create() {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     * POST http://localhost/laravel/users
     */

    public function store() {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     * GET http://localhost/laravel/users/1
     */

    public function show($id) {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */

    public function edit($id) {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     * PUT http://localhost/laravel/users/1
     */

    public function update($id) {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     * DELETE http://localhost/laravel/users/1
     */

    public function destroy($id) {


    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top