2013-01-10 111 views
1

具有以下控制器:Laravel路线REST风格的控制器

class Admin_Images_Controller extends Admin_Controller 
{ 
    public $restful = true; 

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

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

我不明白为什么我的时候不带参数的访问为它工作,因为我得到一个错误说missing parameter for ... ID,但是当我真正尝试通过参数http://site/admin/images/12我得到一个404错误。我错过了什么?

我试着设置在我的路线下,没有成功之一:

Route::any('admin/images', array(
    'as' => 'admin_images', 
    'uses' => '[email protected]', 
)); 
    //or 
Route::any('admin/images/(:any)', array(
    'as' => 'admin_images', 
    'uses' => '[email protected]', 
)); 

它接缝,我带通配符的问题,90%的在我的测试的Linux envirnonment(Ubuntu的)发生。这是我目前使用的routes.php http://pastebin.com/f86A3Usx

回答

2

它可能是你使用相同的别名(admin_images),也检查你的订单 - 首先放置更具体的别名,当你走像这样:

Route::any('admin/images/(:any?)', array('uses' => '[email protected]')); 

已经删除别名,只是为了便于阅读。

+0

随着路线,没有路线,在该顺序路线...它不会去......任何其他方法(post_index,post_myass,put_fireonwate,get_image,get_alife)它去,但不索引:| – Alex

+0

我忘记了问号。检查我的更新并尝试。这应该是有效的 - 如果不行的话,还有其他的东西。 – Oddman

1
Route::get('admin/images/(:any)', '[email protected]'); 
+0

没有效果,它接缝,我只有在我的测试Linux环境(Ubuntu)通配符问题...并在我的电脑上工作正常。这是我完整的routes.php http://pastebin.com/f86A3Usx – Alex

1

你应该让$ id参数可选,通过传递一个默认值(如空/假/ 1)

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

与用途:在你的路线(任意?)。

1

更新的路由:

Route::any('admin/images/(:any?)', array(
    'as' => 'admin_images', 
    'uses' => '[email protected]', 
)); 

你可以通过你的路由合并为每个端点简化路由。通过添加“?”进入你的第一个参数,这意味着任何东西都可以存在,但不一定是。因此,覆盖了/admin/images/admin/images/1234

更新控制器:

class Admin_Images_Controller extends Admin_Controller 
{ 
    public $restful = true; 

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

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

    // ... 
} 

通过添加“= NULL”到你的方法参数,你现在可以处理路由到该功能。在你的方法中简单的检查“等于null”应该可以帮助你覆盖每个参数。