2013-11-03 14:02
浏览 71

Laravel 4 restful controller GET参数

In order to add parameter to a GET method, I know that I have to add {parameter} in the route like the following

Route::get('example/search/{id}', 'ExampleController@exampleMethod')

However, is there a way to do this using the RESTful controller like the following?


Route::controller('example', 'ExampleController')


public function getSearch($id){
    //do something with $id

The above doesn't work because the routes.php doesn't expect a parameter to getSearch method. I wonder if there is a way to solve this without having to add individual Route::get routes.

图片转代码服务由CSDN问答提供 功能建议

为了将参数添加到GET方法,我知道我必须添加 {parameter} < / code>在路由中,如下所示

  Route :: get('example / search / {id}','ExampleController @ exampleMethod')


routes.php < / p>

  Route :: controller('example','ExampleController')

ExampleController.php < / strong>

 公共函数getSearch($ id){
 //用$ id做一些事情
 \  n 

上述方法不起作用,因为 routes.php 不期望 getSearch 方法的参数。 我想知道是否有办法解决这个问题,而无需添加单独的 Route :: get 路由。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanmao9918 2013-11-03 15:41
    // ExampleController.php
    class ExampleController extends BaseController {
        public function getSearch($id = null){
            if ($id == null) {
                return 'no id';
            return $id;
    // routes.php
    Route::controller('example', 'ExampleController');

    php artisan routes:

    enter image description here

    enter image description here

    解决 无用
    打赏 举报

相关推荐 更多相似问题