我今天已经下载了Laravel,我很喜欢它的外观,但是我有两个问题。
1)我喜欢分析url的控制器的动作方法,而不是使用路由,它似乎保持一切在一起更干净,但让我们说我想去
/account/account-year/
我怎么能写一个动作函数?例如
function action_account-year()...
显然是错误的语法。
如果我有function action_account_year( $year, $month ) { ...
和访问
/account/account_year/
将显示关于丢失参数的错误,您如何使此用户友好/加载diff页面/显示错误??
您必须手动路由连字符版本,例如
Route::get('account/account-year', 'account@account_year');
关于参数,这取决于您如何路由。必须接受路由中的参数。如果您使用全控制器路由(例如Route::controller('account')
),则该方法将自动传递参数。
如果您是手动路由,您必须捕获参数,
Route::get('account/account-year/(:num)/(:num)', 'account@account_year');
访问/account/account-year/1/2
会访问->account_year(1, 2)
您还可以考虑以下可能性
class AccountController extends BaseController {
public function getIndex()
{
//
}
public function getAccountYear()
{
//
}
}
现在只需在你的路由文件中以以下方式定义一个RESTful控制器
Route::controller('account', 'AccountController');
访问'account/account-year'
将自动路由到getAccountYear
我想我会添加这个作为答案,以防其他人正在寻找它:
1)
public function action_account_year($name = false, $place = false ) {
if( ... ) {
return View::make('page.error' );
}
}
2)
not a solid solutions yet:
laravel/routing/controller.php, method "response"
public function response($method, $parameters = array())
{
// The developer may mark the controller as being "RESTful" which
// indicates that the controller actions are prefixed with the
// HTTP verb they respond to rather than the word "action".
$method = preg_replace( "#'-+#", "_", $method );
if ($this->restful)
{
$action = strtolower(Request::method()).'_'.$method;
}
else
{
$action = "action_{$method}";
}
$response = call_user_func_array(array($this, $action), $parameters);
// If the controller has specified a layout view the response
// returned by the controller method will be bound to that
// view and the layout will be considered the response.
if (is_null($response) and ! is_null($this->layout))
{
$response = $this->layout;
}
return $response;
}