I have a controller and action which I'm accessing through a custom URL. The original route is still accessible though at the default location
zend.com/controller/action
How can I change this to simulate a "Page not found" when the user tries to access this URL? Is it possible?
If the action handler is used to respond to both URLs, you would first have to detect which URL is being requested (using $this->_request->getRequestUri()). If the default URL is detected I think the easiest way to create a "page not found" would be to use
$this->_redirect("/path/to/simulated/404/page")
and set up a controller and action to respond.
This won't actually send an HTTP 404, though. To do that, I think you would have to raise an exception within your action handler. I don't know what the official "zendy" way of doing this is, but this seems to work:
throw new Zend_Controller_Action_Exception('Not Found', 404);
You could change the main controller script to redirect a certain controller name and action name to a new page. But it's probably easier to add a new rule to the .htaccess
file, indicating that this specific URL should be redirected to an error page. Example:
RewriteRule ^controller/action/?$ / [R=404,L]
Or redirect the page to an error page within your site:
RewriteRule ^controller/action/?$ /error/page-not-found/ [L]
You need to use:
$this->getResponse()->setHttpResponseCode(404);
And build your own 404 view
$this->view->message = 'Page not found';
Or you could forward to an error controller for example
$this->_forward('page-not-found', 'error');
Finally, if you have in your error controller
//...
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found';
break;
//...
You can just do as @bogeymin said:
throw new Zend_Controller_Action_Exception('Not Found', 404);
If you are looking other solutions than mod_rewrite
based, you may create a Regex Route to match the actions you need to hide.
The other solution is to restrict access to Actions using Zend_Acl, treating each action as an ACL resource.
But the simplest and most lightweight solution is still mod_rewrite
in .htaccess
.
As you can see, this may be done in numerous ways. But probably, you will need some kind of the switch, to still allow somehow to access the "hidden" action. In this case, use:
mod_rewrite
for quick implementation (switching requires the person to know the .htaccess
rules)If you don't need to have authenticated users, Zend_Acl
combined with Zend_Router
might be the solution.
For smart handling the exceptions and building ACL's, see this (and other posts on this blog):
Handling errors in Zend Framework | CodeUtopia - The blog of Jani Hartikainen
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With