【问题标题】:Laravel 4. default controller route wont accept argumentsLaravel 4. 默认控制器路由不接受参数
【发布时间】:2014-01-11 08:48:05
【问题描述】:

我有一个 RESTful 控制器供我的用户处理用户个人资料的查看。

问题是这样的:

我希望网址看起来像这样www.example.com/user/1

这将显示 id 为 1 的用户。问题是当我在 UserController 中定义 getIndex 方法时,它不会接受 id 作为参数。

这是我的 routes.php 部分:

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

现在,我的理解是,如果 url 中没有提供任何其他内容,getIndex 是一种默认路由,因此:

public function getIndex() {

}

UserController 内会接受路由,

"www.example.com/user/index" 

"www.example.com/user"

确实如此!

但是,如果我包含一个应该从 url 获取的参数,它就不再起作用了:

public function getIndex($id) {
    //retrieve user info for user with $id
}

这只会响应

"www.example.com/user/index/1" 

而不是

"www.example.com/user/1"

我怎样才能使后者工作?如果没有必要,我真的不想用“索引”这个词来混淆 url。

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

    如果您打算这样做,最好的方法是使用 RESTful 控制器。

    将您的路线更改为这条路线,

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

    然后使用php artisan命令生成控制器,

    php artisan controller:make UserController
    

    这将生成具有所有 RESTful 功能的控制器,

    <?php
    
    class UserController extends \BaseController {
    
        /**
         * Display a listing of the resource.
         *
         * @return Response
         */
        public function index() // url - GET /user (see all users)
        {
            //
        }
    
        /**
         * Show the form for creating a new resource.
         *
         * @return Response
         */
        public function create() 
        {
            //
        }
    
        /**
         * Store a newly created resource in storage.
         *
         * @return Response
         */
        public function store() // url - POST /user (save new user)
        {
            //
        }
    
        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return Response
         */
        public function show($id) // url - GET /user/1 (edit the specific user)
        {
            //
        }
    
        /**
         * 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
         */
        public function update($id) // url - PUT /user/1 (update specific user)
        {
            //
        }
    
        /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return Response
         */
        public function destroy($id) // url - DELETE /user/1 (delete specific user)
        {
            //
        }
    
    }
    

    有关更多信息,请参阅此Laravel RESTful controller parameters

    【讨论】:

      【解决方案2】:

      要在地址栏上显示www.example.com/user/1,您应该使用show 方法。在 Laravel 中,restful 控制器默认创建 7 个路由。秀就是其中之一。

      在您的控制器中创建如下方法:

      public function show($id) 
      {
          // do something with id
      
          $user = User::find($id);
          dd($user);
      }
      

      现在,浏览http://example.com/user/1

      【讨论】:

      • @devo。我的理解是这样的控制器实际上是一个资源控制器,而 RESTful 控制器是这样一种控制器,即每个函数对应一个 HTTP 操作和连接在一起的 URI。在这种情况下,用户控制器中的 public function getProfile() {} 将响应对 .com/user/profile 的 HTTP GET 请求 我的整个用户控制器都设置为使用此方法,我宁愿保持这种方式。不使用资源控制器就没有办法让它工作吗?或者只是在任何控制器中包含一个显示功能?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 2014-11-03
      • 2019-02-06
      • 1970-01-01
      相关资源
      最近更新 更多