user1493920
user1493920

Reputation:

How to remove action part in zend framework URLs

I`m using zend framework and my urls are like this :

http://target.net/reward/index/year/2012/month/11

the url shows that I'm in reward controller and in index action.The rest is my parameters.The problem is that I'm using index action in whole program and I want to remove that part from URL to make it sth like this :

http://target.net/reward/year/2012/month/11

But the year part is mistaken with action part.Is there any way ?!!! Thanks in advance

Upvotes: 0

Views: 643

Answers (2)

Config
Config

Reputation: 1692

Have a look at routes. With routes, you can redirect any URL-format to the controller/action you specify. For example, in a .ini config file, this will do what you want:

routes.myroute.route = "reward/year/:myyear/month/:mymonth"
routes.myroute.defaults.controller = reward
routes.myroute.defaults.action = index
routes.myroute.defaults.myyear = 2012
routes.myroute.defaults.mymonth = 11
routes.myroute.reqs.myyear = "\d+"
routes.myroute.reqs.mymonth = "\d+"

First you define the format the URL should match. Words starting with a colon : are variables. After that you define the defaults and any requirements on the parameters.

Upvotes: 4

M Rostami
M Rostami

Reputation: 4195

you can use controller_plugin to control url .
as you want create a plugin file (/library/plugins/controllers/myplugin.php).
then with preDispatch() method you can get requested url elements and then customize that for your controllers .
myplugin.php

class plugins_controllers_pages extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $int = 0;
        $params = $this->getRequest()->getParams();  
        if($params['action'] != 'index' ) AND !$int)
        {
             $int++;
             $request->setControllerName($params['controller']);
             $request->setActionName('index');
             $request->setParams(array('parameter' => $params['action']));
             $this->postDispatch($request);
        }
    }
}

Upvotes: 0

Related Questions