2013-10-09 208 views
0

这可能是一个简单的问题,但我正在使用Laravel进行测试。我把我的路线是这样相应:现在Laravel 4路由参数REST

// Users Route 
Route::get('users',array('as'=> 'users', 'uses'=> '[email protected]')); 
Route::get('users/{id}', array('as' => 'user', 'uses' => '[email protected]')); 
Route::get('users/{id}/edit', array('as' => 'edit_user', 'uses' => '[email protected]')); 
Route::get('users/new', array('as' => 'new_user', 'uses' => '[email protected]')); 
Route::post('users', '[email protected]'); 
Route::delete('users', '[email protected]'); 

,在我的浏览器,如果我访问localhost/users/new,它会调用一个名为“用户”而不是“new_user”的路线。我的意思是,它会加载编辑路线而不是创建用户。

我的代码有什么问题?

回答

2

优先事项,只是更改为:

Route::get('users',array('as'=> 'users', 'uses'=> '[email protected]')); 
Route::get('users/new', array('as' => 'new_user', 'uses' => '[email protected]')); 
Route::get('users/{id}', array('as' => 'user', 'uses' => '[email protected]')); 
Route::get('users/{id}/edit', array('as' => 'edit_user', 'uses' => '[email protected]')); 
Route::post('users', '[email protected]'); 
Route::delete('users', '[email protected]'); 

Laravel认为 '新' 是你(编号)参数。

+0

我试图用这个代码,当我参观“用户/新”它抛出这个错误:非法抵消类型在isset或空 – Bajongskie

+0

现在你必须看看你的方法UsersController @ create,看起来像你有一个错误。 –

+0

哦,我认为这是我的问题的根源,而不是路线。 – Bajongskie

3

如果您使用RESTful API,那么使用资源路由是最好的。

路线,

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

和控制器

<?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) { 


    } 

} 
+0

+1,因为它消除了manuelly写作路线的头痛。 – itachi