September 21, 2011

How to know route from the URL or $request Object?

Question by mrN

PLOT:

After implementing ACL on my website, if a user tries to access unauthorised page he will be denied and shown a page to login. After he loggs in, I wanted to redirect the user to the previous page to which he was denied to earlier.

To do this, I store the request parameters using $request -> getParams(), onto a session variable, which will be used to generate the url again. This is where the problem occurs, to generate the url back, I need the name of the route and that i dont know how to read.

I need to know the route name, so that I will be able to regenerate the url, from the array stored in session, or if there is a better way to solve this, please suggest.

Answer by Starx

Dont try to think of complex solutions for simple problem.

You can do this, with just using $_SERVER['REQUEST_URI'], this gives the same result as @Phil’s answer (Correct me, If i am missing something). and is more than enough to do what you want.

August 17, 2011

What are the different ways to configure routes?

Question by Starx

If someone is familier with Zend Framewor, they know what routes are and how they affect the system overall. My question is concerned about ways to can configure this routes. I know two ways to configure them, through Bootstrap.php and application.ini.

However, not hiding the fact that, I am pretty much of a learner in Zend Framework myself, I dont know which one is better and which should be preferred over the other.

Moreover, I do not know, if these are only ways available to configure the router?

So, please tell me what are ways through which we can configure router and which method is better over others.

P.S: I have included the two ways I knew as an answer.

Answer by Starx

Since I am attempting this to be as a guide for those like me, I would like to include the two ways I know of.

Application.ini

resources.router.routes.cat.route = "/browse/:catid/:name/"
resources.router.routes.cat.defaults.controller = index
resources.router.routes.cat.defaults.action = browse

Here What you do is,
resources.router.routes.XXX.route Define the name of the route in place of XXX

catid and name are the two paramters that will taken, when you pass the url is such way /browse/1/pc 1 will be assinged to catid and pc to name

Remaining two line defines the default parameter from controller and action, from MVC

Bootstrap.php

    $front = Zend_Controller_Front::getInstance();  
    // Get Router
    $router = $front -> getRouter();
    $routeBrowse = new Zend_Controller_Router_Route(
        '/browse/:catid/:name',
        array(
            'controller' => 'index',
            'action' => 'index'
        )
    );
    $router -> addRoute('browse', $routeBrowse);

I will avoid the explanation, since pretty much is same as before.

However, I am not sure which one is better that the other one. So, those who knows, update my answer.

...

Please fill the form - I will response as fast as I can!