controller & action name

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

controller & action name

jimh
I am trying to get the controller, action, and params from the bootstrap file but can't figure out how to access them, I saw the getControllerName() method in the docs but there isn't much indication on how to use it.  It would be nice to be able to use something like the following so that I can easily implement a custom access control structure that will set the controller/action to an error page if a user does not have access, but first need to be able to know the controller, action and params from the bootstrap file.  Thanks for any help.

$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('controllers/');
echo $controller->getControllerName();
$controller->dispatch();
Reply | Threaded
Open this post in threaded view
|

Re: controller & action name

Arnaud Limbourg
> I am trying to get the controller, action, and params from the bootstrap
> file but can't figure out how to access them, I saw the
> getControllerName() method in the docs but there isn't much indication
> on how to use it.  It would be nice to be able to use something like the
> following so that I can easily implement a custom access control
> structure that will set the controller/action to an error page if a user
> does not have access, but first need to be able to know the controller,
> action and params from the bootstrap file.  Thanks for any help.
>
> $controller = Zend_Controller_Front::getInstance();
> $controller->setControllerDirectory('controllers/');
> echo $controller->getControllerName();
> $controller->dispatch();

You will only be able to know those after the dispatch. Basically one
the myAction() is reached you can do $controllerName =
$this->_action->getControllerName().

For a custom access controller you could either extend the Router or
write a plugin.

--
Arnaud
Reply | Threaded
Open this post in threaded view
|

Re: controller & action name

Michael Patrick-3
In reply to this post by jimh
James Hartford wrote:

> I am trying to get the controller, action, and params from the bootstrap
> file but can't figure out how to access them, I saw the getControllerName()
> method in the docs but there isn't much indication on how to use it.  It
> would be nice to be able to use something like the following so that I can
> easily implement a custom access control structure that will set the
> controller/action to an error page if a user does not have access, but
> first
> need to be able to know the controller, action and params from the
> bootstrap
> file.  Thanks for any help.
>
> $controller = Zend_Controller_Front::getInstance();
> $controller->setControllerDirectory('controllers/');
> echo $controller->getControllerName();
> $controller->dispatch();

Your best bet is going to be to write a plugin for the front controller
I think ( though I don't know if any of that is changing with the new
router coming in 0.1.4 ).

You'd put your access check probably in the preDispatch handler so that
it would be check prior to the target being called and you could change
the target. I do this to see if they are logged in before hitting
certain pages.  If they are, it returns their current action.  If not,
it returns an action aiming for the error controller, general action,
with appropriate message.

public function preDispatch($action) {
        // snipping bit of code to check
        // if page they are going to is on the no-login list

        // check that they are logged in
        switch (UserIsLoggedIn()) {
                case USER_LOGGED_IN:
                        // they are logged in, so let the dispatch go
            return $action;
                break;
                case NOT_LOGGED_IN:
                        return new Zend_Controller_Dispatcher_Token
                                ('error','general',
                                array('message' =>
                                getMessage('ERROR_NOT_LOGGED_IN'));
                case SESSION_EXPIRED:
                        return new Zend_Controller_Dispatcher_Token
                                ('error','general',
                                array('message' =>
                                getMessage('ERROR_SESSION_EXPIRED'));
                break;
                default:
                        return new Zend_Controller_Dispatcher_Token
                                ('error','general');
                }
        }
}

My apologies for the crap formatting.. hope it makes sense in your mailer.

Michael
Reply | Threaded
Open this post in threaded view
|

Re: controller & action name

jimh
Thanks, that looks like what I need to do, but do you have any further info on how to create a plugin?  There doesn't seem to be any documentation on that in the manual.  The code you sent did format fine.

On 6/1/06, Michael Patrick <[hidden email]> wrote:
James Hartford wrote:

> I am trying to get the controller, action, and params from the bootstrap
> file but can't figure out how to access them, I saw the getControllerName()
> method in the docs but there isn't much indication on how to use it.  It
> would be nice to be able to use something like the following so that I can
> easily implement a custom access control structure that will set the
> controller/action to an error page if a user does not have access, but
> first
> need to be able to know the controller, action and params from the
> bootstrap
> file.  Thanks for any help.
>
> $controller = Zend_Controller_Front::getInstance();
> $controller->setControllerDirectory('controllers/');
> echo $controller->getControllerName();
> $controller->dispatch();

Your best bet is going to be to write a plugin for the front controller
I think ( though I don't know if any of that is changing with the new
router coming in 0.1.4 ).

You'd put your access check probably in the preDispatch handler so that
it would be check prior to the target being called and you could change
the target. I do this to see if they are logged in before hitting
certain pages.  If they are, it returns their current action.  If not,
it returns an action aiming for the error controller, general action,
with appropriate message.

public function preDispatch($action) {
        // snipping bit of code to check
        // if page they are going to is on the no-login list

        // check that they are logged in
        switch (UserIsLoggedIn()) {
                case USER_LOGGED_IN:
                        // they are logged in, so let the dispatch go
                        return $action;
                break;
                case NOT_LOGGED_IN:
                        return new Zend_Controller_Dispatcher_Token
                                ('error','general',
                                array('message' =>
                                getMessage('ERROR_NOT_LOGGED_IN'));
                case SESSION_EXPIRED:
                        return new Zend_Controller_Dispatcher_Token
                                ('error','general',
                                array('message' =>
                                getMessage('ERROR_SESSION_EXPIRED'));
                break;
                default:
                        return new Zend_Controller_Dispatcher_Token
                                ('error','general');
                }
        }
}

My apologies for the crap formatting.. hope it makes sense in your mailer.

Michael

Reply | Threaded
Open this post in threaded view
|

Re: controller & action name

Axel Christ
James Hartford wrote:
> Thanks, that looks like what I need to do, but do you have any further
> info on how to create a plugin?  There doesn't seem to be any
> documentation on that in the manual.  The code you sent did format fine.
>
You can extend Zend_Controller_Plugin_Abstract and then register it as a
plugin by using the registerPlugin() method of Zend_Controller_Front. If
you don't already have done so: it is a good idea to download the source
of the Zend framework. It is very organized and looking into it makes
many things much more clear then just reading the documentation.

Best regards

Axel
Reply | Threaded
Open this post in threaded view
|

Re: controller & action name

Ryan Lange
Axel Christ wrote:
> You can extend Zend_Controller_Plugin_Abstract and then register it as a
> plugin by using the registerPlugin() method of Zend_Controller_Front.

     Just to offer a little more detail, you have to register an
instance of your plugin before the dispatch, like so:

         Zend_Controller_Front::getInstance()
                 ->setControllerDirectory( 'controllers/' )
                 ->registerPlugin( new Plugin() )
                 ->dispatch();

Ryan