Add Zend_Controller_Router_Route replaced default route - php

When I added this route
$classes_router = new Zend_Controller_Router_Route(
'/:filter1/:filter2/*',
array(
'module' => 'course',
'controller' => $filter_controller,
'action' => 'index',
'filter1' => '',
'filter2' => ''
)
);
Te default route :module/:controller/:action doesn't work anymore. Please tell me what is the problem?

The problem is that a request to somemodule/somecontroller/someaction will be matched by the route you've added (which will be checked before the default one). You need to provide some restriction in the route to determine what it matches, perhaps by limiting the possible matches for the :filter1 variable:
$classes_router = new Zend_Controller_Router_Route(
'/:filter1/:filter2/*',
array(
'module' => 'course',
'controller' => $filter_controller,
'action' => 'index',
'filter1' => '',
'filter2' => ''
), array(
'filter1' => '(value1|foo|somethingelse)'
)
);
or adding a static prefix:
$classes_router = new Zend_Controller_Router_Route(
'/filter/:filter1/:filter2/*',
array(
'module' => 'course',
'controller' => $filter_controller,
'action' => 'index',
'filter1' => '',
'filter2' => ''
)
);

Related

Phalcon two single routes

I have a problem. I have two routes:
$router->add('/news/{alias:[a-z\-]+}(/?)', array(
'module' => 'frontend',
'controller' => 'news',
'action' => 'view',
'news_id' => 1,
'lang' => 'md',
))->setName('news_view_short_e'); //=> /news/282334-alias-news
AND route => /news/index/:
$router->add('/{lang:[' . $langsDefined . ']{2}+}/{controller:[a-z]{3,50}+}(/?)', array(
'module' => 'frontend',
'controller' => 2,
'action' => 'index',
'lang' => 1,
))->setName('default_module_lang');
When I use these routes. site.com/news/index not working. But if I remove route with alias. Route site.com/news/index working good. How I can resolve conflict?
Probably because it matches your "index" as alias parameter too, so first rule wins. There's no magic solution other than just fixing your regex or reordering your routes.
Reversing your routes order will bring a new problem though, reversely because it will also match the controller instead of your alias.
I prefer to stick with the default routing, and mount a router group here's my router right now:
//From my config
'router' => array(
'defaults' => array(
'namespace' => 'My\\Default\\Namespace\\Controllers',
'module' => 'frontend',
'controller' => 'index',
'action' => 'index'
),
'notFound' => array(
'controller' => 'errors',
'action' => 'notFound'
)
),
// From my service
$router = new Router();
$router->removeExtraSlashes(true);
$router->setDefaults($this->config->router->defaults->toArray()?: $this->defaults);
$router->notFound($this->config->router->notFound->toArray()?: $this->notFound);
$router->mount(new ModuleRoute($this->getDefaults(), true));
class ModuleRoute extends RouterGroup
{
public $default = false;
public function __construct($paths = null, $default = false)
{
$this->default = $default;
parent::__construct($paths);
}
public function initialize()
{
$path = $this->getPaths();
$routeKey = '/' . $path['module'];
$this->setPrefix('/{locale:([a-z]{2,3}([\_\-][[:alnum:]]{1,8})?)}' . ($this->default ? null : $routeKey));
$this->add( '/:params', array(
'namespace' => $path['namespace'],
'module' => $path['module'],
'controller' => $path['controller'],
'action' => $path['action'],
'params' => 3
))->setName($routeKey);
$this->add( '/:controller/:params', array(
'namespace' => $path['namespace'],
'module' => $path['module'],
'controller' => 3,
'action' => $path['action'],
'params' => 4
))->setName($routeKey);
$this->add( '/:controller/([a-zA-Z0-9\_\-]+)/:params', array(
'namespace' => $path['namespace'],
'module' => $path['module'],
'controller' => 3,
'action' => 4,
'params' => 5
))->setName($routeKey);
$this->add( '/:controller/:int', array(
'namespace' => $path['namespace'],
'module' => $path['module'],
'controller' => 3,
'id' => 4,
))->setName($routeKey);
}
}

Social Engine 4 User profile rerouting

For Social Engine 4, which uses Zend framework.
I want to show user profiles under subdomains. so, http://domain.com/profile/username will be http://username.domain.com
How can I do this. I do already have one extension which makes http://domain.com/profile/username to http://domain.com/username Here is the code for it.
$route = array(
'user_profile' => array(
'route' => ':id/*',
'defaults' => array(
'module' => 'user',
'controller' => 'profile',
'action' => 'index'
),
'reqs' => array(
'id' => "\b.+"
)
)
);
Zend_Registry::get('Zend_Controller_Front')->getRouter()->addConfig(new Zend_Config($route));
Is it possible to change it to work for subdomains? If yes how can I do it?
Thank You in advance.
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':id.'.$setting['siteurl'],
array(
'module' => 'user',
'controller' => 'profile',
'action' => 'index',
)
);
$plainPathRoute = new Zend_Controller_Router_Route_Static('');
$router->addRoute('user_profile', $hostnameRoute->chain($plainPathRoute));

How to redirect requests to www.example.com/admin to different module in Zend

I have a problem with redirecting requests in my application.
My rules:
employer.domain.com - should point to a page of the employer - uses default module
employer.domain.com/panel/ - should point to a administration page of specific employer - uses dashboard module
www.domain.com - should point to page aggregating all employers - uses default module
I've tested a lot of different routes, but when one route is working, other get broken. Also often it works only for root paths, but when I add call to some controller and action - it crashes. Maybe I should write custom controller plugin? What do You think?
Here is my current configuration. It's a mess, but maybe it will help catching some silly mistake.
// employer.domain.com/panel
$pathRoute_panel = new Zend_Controller_Router_Route(
':panel/:controller/:action/:id/',
array(
'panel' => '',
'module' => 'dashboard',
'controller' => 'index',
'action' => 'index',
'id' => '',
),
array(
'panel' => 'panel'
)
);
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
':employer.'.$config['host'],
null,
array(
'employer' => '([a-z0-9]+)',
)
);
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel));
// employer.domain.com - main employer page
$pathRoute_panel = new Zend_Controller_Router_Route(
'',
array(
'module' => 'default',
'controller' => 'vcard',
'action' => 'index',
'id' => '',
)
);
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
':employer.'.$config['host'],
null,
array(
'employer' => '([a-z0-9]+)',
)
);
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel));
// domain.com/
$pathRoute = new Zend_Controller_Router_Route_Module(
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
),
$dispatcher,
$request
);
$route = new Zend_Controller_Router_Route_Hostname($config['host']);
$router->addRoute('default', $route->chain($pathRoute));
// www.domain.com/
$pathRoute = new Zend_Controller_Router_Route_Module(
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
),
$dispatcher,
$request
);
$route = new Zend_Controller_Router_Route_Hostname('www.'.$config['host']);
$router->addRoute('default_www', $route->chain($pathRoute));
EDIT: This is my solution:
// employer.domain.com
$pathRoute_panel = new Zend_Controller_Router_Route_Module(
array(
'module' => 'default',
'controller' => 'vcard',
'action' => 'index',
'id' => '',
)
);
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
':employer.'.$config['host'],
null,
array(
'employer' => '([a-z0-9]+)',
)
);
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel));
// employer.domain.com/panel/
$pathRoute_panel = new Zend_Controller_Router_Route(
'panel/:controller/:action/:id/',
array(
'panel' => 'panel',
'module' => 'dashboard',
'controller' => 'index',
'action' => 'index',
'id' => '',
)
);
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
':employer.'.$config['host'],
null,
array(
'employer' => '([a-z0-9]+)',
)
);
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel));
// Enforce Subdomain usage
// Will match employer.domain.com/*
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
'employer.'.$config['host'] . '/*'
);
// Will match /panel/{id, required}/{controller, optional}/{action, optional}/{further parameters, optional}
$pathRoute_panel = new Zend_Controller_Router_Route(
'panel/:id/:controller/:action/*',
array(
'module' => 'dashboard',
'controller' => 'index',
'action' => 'index'
),
array(
'id' => '\d+'
)
);
// employer.domain.com - main employer page
// will match everything not matched before!
$pathRoute_page = new Zend_Controller_Router_Route(
'*',
array(
'module' => 'default',
'controller' => 'vcard',
'action' => 'index'
)
);
// You can add several routes to one chain ;) The order does matter.
$subDomainRoute->chain($pathRoute_page);
$subDomainRoute->chain($pathRoute_panel);
$router->addRoute($subDomainRoute);
Annotations are in the code. You can use the wildcard (*) to match anything further! There's no need of the default_www route - this's just the default behaviour (the default route now will match every subdomain but employer as it is matched by the subDomainRoute).

ZF: routing. How to make correct route?

I have several templates:
/admin/
/somecode1/somecode2/
/staticpage.htm
Trying to enter on /admin/. Doing follow:
$router->addRoutes(array(
'adminsys' => new Zend_Controller_Router_Route('/:module/:controller/:action/*', array('module' => 'admin', 'controller' => 'index', 'action' => 'index')),
'page_catalog' => new Zend_Controller_Router_Route('/:code/:page', array('module' => 'default', 'controller' => 'Staticcatalog', 'action' => 'index', 'code' => '', 'page' => '')),
'static' => new Zend_Controller_Router_Route_Regex('([\wА-Яа-я\-\_]+)\.(htm|html)', array('module' => 'default', 'controller' => 'static', 'action' => 'index')
));
also I tried to change 'adminsys' on :
'adminsys' => new Zend_Controller_Router_Route('/admin/:controller/:action/*', array('module' => 'admin', 'controller' => 'index', 'action' => 'index')),
or
'adminsys' => new Zend_Controller_Router_Route('/admin/*', array('module' => 'admin', 'controller' => 'index', 'action' => 'index')),
But all time it routes on 'page_catalog'.
If I comment it, I can enter /admin/. But not with 'page_catalog'.
What I'm doing wrong here?
When you define routes, you define the general one first, and then you go more and more specific after. The router takes your routes 'last one first' and stops on the first that matches.
This means that if '/admin' could also work for the 'page_catalog' route, it would use this one, before even trying to match 'adminsys' route. And that's the thing, '/admin' could be a 'page_catalog' url where :code param would be 'admin'.
The second variant of the adminsys route is a good one, you just have to make it the last one of your routes in order to avoid any more general route to match first :
$router->addRoutes(array(
'page_catalog' => new Zend_Controller_Router_Route('/:code/:page', array('module' => 'default', 'controller' => 'Staticcatalog', 'action' => 'index', 'code' => '', 'page' => '')),
'static' => new Zend_Controller_Router_Route_Regex('([\wА-Яа-я\-\_]+)\.(htm|html)', array('module' => 'default', 'controller' => 'static', 'action' => 'index')),
'adminsys' => new Zend_Controller_Router_Route('/admin/:controller/:action/*', array('module' => 'admin', 'controller' => 'index', 'action' => 'index'))
));

Zend_Controller_Router_Route

I'm trying to make a Router that can respond to this structure:
module/controller/action/id and module/controller/action/page
The only difference is is 'id' or 'page'. I'm using this code:
$routeAdmin = new Zend_Controller_Router_Route(
'administrador/:controller/:action/:id/:pg',
array(
'module' => 'administrador',
'controller' => 'index',
'action' => 'index',
'id' => 0,
'pg' => 1
),
array(
'id' => '\d+',
'pg' => '\d+'
)
);
$router->addRoute('administrador', $routeAdmin);
The problem is that in some situations i want:
'http://www.domain.cl/administrador/productos/2' => (module=>administrador, controller=>productos,page=>2) but with the router 'administrador' result in 'http://www.domain.cl/administrador/productos/index/0/2' (module=>administrador, controller=>productos,action=>index,id=>0,page=>2)
I'm very confused about how it works for cases like this. I tried to make two router where the first only have 'id' param and the other 'page' param. And from url helper use it like:
$this->url(array('module' => 'administrador', 'controller' => 'productos', 'action' => 'index', 'id' => 0), 'administradorId');
$this->url(array('module' => 'administrador', 'controller' => 'productos', 'action' => 'index', 'page' => 1), 'administradorPg');
But when I used the routers always select the last one added to the router ($router->addRoute('routerIdentifier', $route);)
Thanks
I have had a similar issue and I got around this by defining just one route like this
$routeAdmin = new Zend_Controller_Router_Route(
'administrador/:controller/:action/:id/:pg',
array(
'module' => 'administrador',
'controller' => 'index',
'action' => 'index',
'id' => 0
),
array(
'id' => '\d+'
)
);
$router->addRoute('administrador', $routeAdmin);
In your actions you'd then need to get the id and check for it somewhere where that id might be, for example if you were in /administrador/events/view/1 then you might look in the events table or if you were in /administrador/pages/view/1 then you would look for a page.
But the problems really start when the id could be either an event or a page in a given controller and action. The only real way around this is explicitly set the type of id your using for example
/administrador/events/view/index/id/1
or
/administrador/pages/view/index/page/1
If you want to remove the index part then set up routes like
$routeAdmin = new Zend_Controller_Router_Route(
// Remove the action from here and explicitly set the controller
'administrador/pages/:pg',
array(
'module' => 'administrador',
'controller' => 'pages',
// Then set the default action here
'action' => 'index',
'pg' => 0
),
array(
'pg' => '\d+'
)
);
$router->addRoute('administradorpages', $routeAdmin);
What your asking for basically results in a lot of guess work and therefore risk of producing unexpected results.
Have a look at dynamic segments in routes. It is not exactly what you want, but it might be helpful in your case.

Categories