Laravel 中的 RESTful 控制器和路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12289827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RESTful controllers and routing in laravel
提问by user61629
I am coming from codeigniter, and trying to wrap my head around routing. I am following the http://codehappy.daylerees.com/using-controllerstutorial
我来自 codeigniter,并试图围绕路由进行思考。我正在关注 http://codehappy.daylerees.com/using-controllers教程
If you scroll down to RESTful controllers, Dayle talks about Home_Controller extending the base_controller and adding public function get_index() and post_index(). I have copied the code, but when I go to
如果向下滚动到 RESTful 控制器,Dayle 会谈到 Home_Controller 扩展 base_controller 并添加公共函数 get_index() 和 post_index()。我已经复制了代码,但是当我去
http://localhost/m1/public/account/superwelcome/Dayle/Wales
I get:
我得到:
We took a wrong turn. Server Error: 404 (Not Found).
我们走错了弯。服务器错误:404(未找到)。
Is there anything obvious that I'm doing wrong ? Should I be putting the code somewhere else? Here's my code
有什么明显的我做错了吗?我应该把代码放在其他地方吗?这是我的代码
class Base_Controller extends Controller {
/**
* Catch-all method for requests that can't be matched.
*
* @param string $method
* @param array $parameters
* @return Response
*/
public function __call($method, $parameters)
{
return Response::error('404');
}
public $restful = true;
public function get_index()
{
//
}
public function post_index()
{
//
}
}
In the routes.php file I have:
在routes.php文件中,我有:
// application/routes.php
Route::get('superwelcome/(:any)/(:any)', 'account@welcome');
my account controller ( from the tutorial) is:
我的帐户控制器(来自教程)是:
// application/controllers/account.php
class Account_Controller extends Base_Controller
{
public function action_index()
{
echo "This is the profile page.";
}
public function action_login()
{
echo "This is the login form.";
}
public function action_logout()
{
echo "This is the logout action.";
}
public function action_welcome($name, $place)
{
$data = array(
'name' => $name,
'place' => $place
);
return View::make('welcome', $data);
}
}
回答by akhy
You should change the line in application/controllers/account.php
你应该改变行 application/controllers/account.php
public function action_welcome($name, $place)
to
到
public function get_welcome($name, $place)
since the Account_Controller inherits $restful = TRUEfrom Base_Controller class, making action_-prefixed function name unusable.
由于 Account_Controller 继承$restful = TRUE自 Base_Controller 类,使得action_-prefixed 函数名不可用。
Additionally, you must change all the function prefixes in account.phpto get_for the same reason :)
此外,你必须改变所有的功能前缀中account.php来get_出于同样的原因:)

