EDIT:
Так, как @Darsstar предложил в комментарии, что лучше изменить действие в перед методом в контроллере, чем для переопределения метода выполнения. Так он идет, как это, в контроллере пользователя:
protected $default_action = 'index';
public function before()
{
$action = 'action_'.$this->request->action();
if (!empty($this->default_action) && !method_exists($this, $action))
{
$this->request->action($this->default_action);
}
}
Так что, если нет текущего действия действия и по умолчанию определяется она изменяет текущее действие запроса по умолчанию. Вы можете поместить этот код в главный контроллер и определить только $ default_action в субконтроллерах.
Старый ответ:
Вы должны переопределить выполнить метод из класса контроллера. Обычно это выглядит так:
public function execute()
{
// Execute the "before action" method
$this->before();
// Determine the action to use
$action = 'action_'.$this->request->action();
// If the action doesn't exist, it's a 404
if (! method_exists($this, $action))
{
throw HTTP_Exception::factory(404,
'The requested URL :uri was not found on this server.',
array(':uri' => $this->request->uri())
)->request($this->request);
}
// Execute the action itself
$this->{$action}();
// Execute the "after action" method
$this->after();
// Return the response
return $this->response;
}
Изменить его на что-то вроде этого:
public function execute()
{
// Execute the "before action" method
$this->before();
// Determine the action to use
$action = 'action_'.$this->request->action();
// If the action doesn't exist, check default action
if (! method_exists($this, $action))
{
//Can be hardcoded action_index or $this->default_action set in controller
$action = 'action_index';
// If the action doesn't exist, it's a 404
if (! method_exists($this, $action))
{
throw HTTP_Exception::factory(404,
'The requested URL :uri was not found on this server.',
array(':uri' => $this->request->uri())
)->request($this->request);
}
}
// Execute the action itself
$this->{$action}();
// Execute the "after action" method
$this->after();
// Return the response
return $this->response;
}
Теперь, если действие не существует, она проверяет действие по умолчанию и запускает его, или, если не существует бросками 404 .
Изменение действия в методе before() было бы лучше. Это сделано для таких вещей. – Darsstar
Хорошо, это имеет смысл. Не думал об этом ... – f1ames