Skip to content
Advertisement

Zend redirect to controller if action not exist

For example I have action example.com/books/list

But I made mistake and write example.com/books/lists, action lists not exist, how to redirect to controller/index if action not exist.

To check before go to action I can use preDispatch(), but how to check do this action exist ?

Advertisement

Answer

Here is a function to check weither an action exists or not. It takes as parameter Zend_Controller_Request_Abstract and returns a boolean :

JavaScript

Pleaser see this link for more details.

And then if the action effectively does not exists (the function returns false), redirect to your index route :

JavaScript

EDIT :

AS @MuhannadA.Alhariri and @php-dev state respectively in their answer and comment, this can be also handled by customizing the ErrorController within we have just to compare the error_handler with Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION. Here is a post which gives a customized error controller.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement