简体   繁体   English

RESTful控制器的Laravel路由

[英]Laravel routes for RESTful controllers

Having the following controller: 具有以下控制器:

class Admin_Images_Controller extends Admin_Controller
{
    public $restful = true;

    public function __construct()
    {
        parent::__construct();
    }

    public function get_index($id)
    {
        echo $id;
    }

I don't understand why when I access it with no parameter for ID it works, as I get an error says missing parameter for ... but when I actually try to pass a parameters at http://site/admin/images/12 I get a 404 error. 我不明白为什么我在不使用ID参数的情况下访问它的原因是什么,因为我收到一条错误消息,表示... missing parameter for ...但是当我实际上尝试在http://site/admin/images/12传递参数时http://site/admin/images/12我收到404错误。 What am I missing? 我想念什么?

I tried setting the following in my routes, no success either: 我尝试在路线中设置以下内容,也未成功:

Route::any('admin/images', array(
    'as' => 'admin_images',
    'uses' => 'admin.images@index',
));
    //or 
Route::any('admin/images/(:any)', array(
    'as' => 'admin_images',
    'uses' => 'admin.images@index',
));

It seams that my issues with wildcards, 90% happen in my test linux envirnonment (ubuntu). 它表明我的通配符问题有90%发生在我的测试linux环境(ubuntu)中。 Here's my routes.php that I'm currently using http://pastebin.com/f86A3Usx 这是我当前正在使用的http://pastebin.com/f86A3Usx

it could be that you're using the same alias (admin_images) and also, check your order - put the more specific ones first, and more generic as you go down, like so: 可能是因为您使用的是相同的别名(admin_images),并且还检查了订单-将更具体的订单放在首位,并在登录时使用更通用的订单,例如:

Route::any('admin/images/(:any?)', array('uses' => 'admin.images@index'));

Have removed the alias, just for readability. 删除了别名,只是为了便于阅读。

Route::get('admin/images/(:any)', 'admin.images@index');

You should make the $id parameter optional, by passing a default value (like null/false/1) 您应该通过传递默认值(例如null / false / 1)来使$ id参数为可选参数

public function get_index($id = null)
{
    if($id){ 
        echo $id;
    }else{
        echo "No ID given!";
    }   
}

And use (:any?) in your route. 并在您的路线中使用(:any?)。

Updated Routes: 更新路线:

Route::any('admin/images/(:any?)', array(
    'as' => 'admin_images',
    'uses' => 'admin.images@index',
));

You can simplify your routing by combining your routes for each endpoint. 您可以通过组合每个端点的路由来简化路由。 By adding the "?" 通过添加“?” into your first parameter, this mean that anything can be present, but doesn't have to be. 到您的第一个参数中,这意味着任何事物都可以存在,但不一定必须存在。 So both /admin/images and /admin/images/1234 are covered. 因此, / admin / images/ admin / images / 1234均已涵盖。

Updated Controller: 更新的控制器:

class Admin_Images_Controller extends Admin_Controller
{
    public $restful = true;

    public function __construct()
    {
        parent::__construct();
    }

    public function get_index($id=null)
    {
        echo $id;
    }

    // ...
}

With the addition of "= null" into your method parameter, you can now handle both routes into this function. 通过在方法参数中添加“ = null”,您现在可以将两条路由都处理到该函数中。 A simple check of "equals null" within your method should put you well on your way to covering each senario. 在您的方法中对“等于空”的简单检查将使您很好地覆盖每个senario。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM