我来自codeigniter,并试图绕过路由.我正在关注 http://codehappy.daylerees.com/using-controllers教程
如果向下滚动到RESTful控制器,Dayle将讨论Home_Controller扩展base_controller并添加公共函数get_index()和post_index().我复制了代码,但是当我去的时候
http://localhost/m1/public/account/superwelcome/Dayle/Wales
Run Code Online (Sandbox Code Playgroud)
我明白了:
我们走错了路.服务器错误: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()
{
//
}
}
Run Code Online (Sandbox Code Playgroud)
在routes.php文件中,我有:
// application/routes.php
Route::get('superwelcome/(:any)/(:any)', 'account@welcome');
Run Code Online (Sandbox Code Playgroud)
我的帐户控制器(来自教程)是:
// 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);
}
}
Run Code Online (Sandbox Code Playgroud)
你应该改变一行 application/controllers/account.php
public function action_welcome($name, $place)
Run Code Online (Sandbox Code Playgroud)
至
public function get_welcome($name, $place)
Run Code Online (Sandbox Code Playgroud)
由于Account_Controller继承$restful = TRUE自Base_Controller类,因此使得action_-prefixed函数名不可用.
此外,你必须改变所有的功能前缀中account.php来get_出于同样的原因:)