Phalcon two single routes - php

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);
}
}

Related

Using Router Annotations with other routes in Phalcon

Is it possible to use annotations on top of regular routes in Phalcon. For example, I have this:
$router->add('/', [
'module' => 'home',
'controller' => 'index',
'action' => 'index'
])->setName('home');
$router->notFound([
'module' => 'home',
'namespace' => 'Home\Controller',
'controller' => 'error',
'action' => 'show404'
]);
foreach ($app->getModules() as $key => $module) {
$router->add('/'.$key.'/:params', [
'module' => $key,
'controller' => 'index',
'action' => 'index',
'params' => 1
])->setName($key);
$router->add('/'.$key.'/:controller/:params', [
'module' => $key,
'controller' => 1,
'action' => 'index',
'params' => 2
]);
$router->add('/'.$key.'/:controller/:action/:params', [
'module' => $key,
'controller' => 1,
'action' => 2,
'params' => 3
]);
}
$router->addModuleResource('api', 'Api\\Controller\\Index', '/api/index');
$router->addModuleResource('api', 'Api\\Controller\\Client', '/api/client');
The problem is that the router annotations that I define near the bottom don't seem to work, probably because the routes to /api/index and /api/client have already been defined above.
Also, at the same time, is there a way to define a notFound for a module? Basically, if I'm at some URL that starts with /api/, I would like it to go to an error page in my api module. Otherwise, it should go to error page in the regular module.

Phalcon URL assembly - how to supply parameters

I have the following Phalcon route:
$router->add('/:controller/:action/:params', [
'module' => 'secured',
'controller' => 1,
'action' => 2,
'params' => 3,
'namespace' => 'My\Namespace\Controllers'
])->setName('main');
I am trying to assemble URL for that route, which should look like this:
/user/register/admin/john
Where, "user" is controller name, "register" is action and there are two params: [0] = 'admin', [1] = 'john'.
I am assembling it as follows:
$url = $this->di['url']->get([
'for' => 'main',
'controller' => 'user',
'action' => 'regiser',
'params' => [
'admin',
'john'
]
]);
However, parameters are not in the $url:
/user/register
How can I make :params go into final URL?
Thanks!
I cannot check this right now, but does:
array(
'for' => 'main',
'controller' => 'user',
'action' => 'regiser',
'admin'
'john'
);
works as you wish?
I found the only way how to do this. Do it myself:
$this->url->get(array(
'for' => 'main',
'controller' => 'user',
'action' => 'register',
'params' => implode("/", array('admin', 'john'))
));

First optional routing segment acts as mandatory one for child routes

I have the following routing definition:
'admin_default' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:lang]/administrator[/:module][/:action]',
'constraints' => array(
'lang' => '[a-zA-Z]{2}',
'module' => '[a-zA-Z0-9_-]*',
'action' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'module' => 'Application',
'controller' => 'Admin',
'action' => 'index',
'lang' => 'ru'
),
),
'may_terminate' => true,
'child_routes' => array(
'wildcard' => array(
'type' => 'wildcard',
'may_terminate' => true,
'options' => array(
'key_value_delimiter' => '/',
'param_delimiter' => '/'
),
),
),
),
So, I can't get rid of segment [/:lang] in URL string
For example:
URL view helper $this->url('admin_default', array('module' => 'albums')) returns the following URL string:
/administrator/albums
while $this->url('admin_default/wildcard', array('module' => 'albums', 'action' => 'edit', 'id' => album_id_here)) returns:
/ru/administrator/albums/edit/id/album_id_here
How can I remove [/:lang] segment from URL string in second case?
so whats the matter with that "ru" ?
you have to extend the zend view helper URL to inject your current locale to the URL
look what i made for my current project :
<?php
namespace PatrickCore\View\Helper;
use Doctrine\ORM\EntityManager;
use Zend\View\Helper\Url;
class I18nUrl extends Url {
/**
* #var String
*/
protected $lang;
protected $router;
public function __construct($locale,$router) {
$arraylanguagemapping = array(
'en_US' => 'en',
'fa_IR' => 'fa'
);
$this->lang = $arraylanguagemapping[$locale];
$this->router = $router;
}
public function __invoke($name = null, array $params = array(), $options = array(), $reuseMatchedParams = false) {
$this->setRouter($this->router);
if (!array_key_exists('lang', $params)) {
$params['lang'] = $this->lang;
}
return parent::__invoke($name,$params,$options,$reuseMatchedParams);
}
}
?>

ZendFramework 2 - How to make all the actions available with one router rule? Its only allowing action index

How to allow all sub actions inside that controller with one router rule? For example this follow:
visit: site/login - works only
site/login/forgetpassword - does not work
site/login/remmeberme - does not work
Example:
$router = $e->getApplication()->getServiceManager()->get('router');
$route = Http\Literal::factory(array(
'route' => '/login',
'defaults' => array(
'controller' => 'Application\Controller\Login',
'action' => 'index'
),
));
$router->addRoute('login', $route, null);
Follow up:
How can i make it so that /login and /login/anything works?
$route = Http\Segment::factory(array(
'route' => '/login[/:action]',
'defaults' => array(
'controller' => 'Application\Controller\Login',
'action' => 'index'
),
));
$router->addRoute('login', $route, null);
There is an excellent QuickStart Tutorial available within the official Documentation. Set up your route like the following to be allowed multiple actions and an ID Parameter. Fur further information please take a look at the documentation.
You may also be interested in DASPRiDs presentation from ZendCon2012
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),

Add Zend_Controller_Router_Route replaced default route

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' => ''
)
);

Categories