[英]RESTful controllers and routing in laravel
I am coming from codeigniter, and trying to wrap my head around routing. 我来自codeigniter,并试图绕过路由。 I am following the http://codehappy.daylerees.com/using-controllers tutorial
我正在关注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(). 如果向下滚动到RESTful控制器,Dayle将讨论Home_Controller扩展base_controller并添加公共函数get_index()和post_index()。 I have copied the code, but when I go to
我复制了代码,但是当我去的时候
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);
}
}
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 = TRUE
from Base_Controller class, making action_
-prefixed function name unusable. 因为Account_Controller从Base_Controller类继承
$restful = TRUE
,所以使action_
-prefixed函数名不可用。
Additionally, you must change all the function prefixes in account.php
to get_
for the same reason :) 此外,您必须将
account.php
所有函数前缀更改为get_
,原因相同:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.