我来自 codeigniter,并试图将我的头围绕在路由上。 我正在关注 http://codehappy.daylerees.com/using-controllers教程
如果向下滚动到 RESTful Controller ,Dayle 会谈到 Home_Controller 扩展 base_controller 并添加公共(public)函数 get_index() 和 post_index()。我已经复制了代码,但是当我去
http://localhost/m1/public/account/superwelcome/Dayle/Wales
我得到:
我们走错了路。服务器错误:404(未找到)。
有什么明显的地方我做错了吗?我应该把代码放在别的地方吗?这是我的代码
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()
{
//
}
}
在我的 routes.php 文件中:
// application/routes.php
Route::get('superwelcome/(:any)/(:any)', 'account@welcome');
我的帐户 Controller (来自教程)是:
// 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);
}
}
最佳答案
您应该更改 application/controllers/account.php
中的行
public function action_welcome($name, $place)
到
public function get_welcome($name, $place)
由于 Account_Controller 继承自 Base_Controller 类的 $restful = TRUE
,使得以 action_
为前缀的函数名称无法使用。
此外,出于同样的原因,您必须将 account.php
中的所有函数前缀更改为 get_
:)
关于php - laravel 中的 RESTful Controller 和路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12289827/