Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set controller/action to Page Not Found

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?

like image 696
jblue Avatar asked Oct 03 '10 12:10

jblue


4 Answers

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);
like image 182
bogeymin Avatar answered Nov 02 '22 23:11

bogeymin


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]
like image 38
Alec Avatar answered Nov 02 '22 23:11

Alec


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);
like image 28
Keyne Viana Avatar answered Nov 02 '22 23:11

Keyne Viana


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.

Edit:

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)
  • Zend_Router - the person who knows the right route can still access the feature
  • Zend_Acl + Zend_Auth for scalable and secure solution.

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

like image 23
takeshin Avatar answered Nov 02 '22 23:11

takeshin