I have many modules and depending upon modules routes are generated dynamically. For explaining purpose I m adding route for just one module. Following are the routes for the users module.
<?php
//1
$router->add('/users',array(
'module' =>'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index'
));
//2
$router->add('/users/:params/',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index',
'params'=>1
));
//3
$router->add('/users/:action',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1
));
//4
$router->add('/users/:action/:params',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1,
'params'=>2
));
?>
Lets say the url for the user module is
http://www.example.com/admin/users/
This url matches the very first route and its working as expected. But when we navigate to next page, my url looks like
http://www.example.com/admin/users/2
now the problem is it should match 2nd route, but it matches 4th route. If I move 2nd route all the way down, the above url work, but the url
http://www.example.com/admin/users/search/1 will not work
Can anybody help me make it work?
Thanks
:params is tricky because it will match anything. So /users/:params/ matches both /users/:action and /users/:action/:params, which itself has :params in it - making it a routing mind blow.
As a general rule avoid the mind blow scenario. For example you can put :params at the end of the longest possible match (/users/:action/:params) and then rewrite the shorter routes without any :params in them.
Related
Hy stackoverflow !
I'm trying to make a form into an external page with Yii 1.1.14 (this is an old site).
I've made a directory into my views call signinPartners and into this one, a php file call signin.
I have also created a controller :
class SigninPartnerController extends Controller
{
public function actionSignin(){
$this->render('/signin');
}
}
He renders the route /signin defined in my config by :
return array(
'' => 'index',
'signin' => 'signinPartners/signin',
);
but when I try the URL http://mylocalserver/signin the site send back Error 404 Unable to resolve the request « signin-partners/signin »..
This is really disturbing because there are other URL's on my website and they work the same way without throwing an error 404. I don't know what I missed... Can somebody help ?
my urlManager :
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false, // do not display index.php in the url
'urlSuffix' => '/',
'rules' => $routesRules, //this variable contains the array defined above
),
I have also check my runtime :
2018/01/17 11:12:16 [error] [exception.CHttpException.404] exception 'CHttpException' with message 'Impossible de résoudre la requête « signinPartners/signin ».' in D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\library\Yii\web\CWebApplication.php:286
Stack trace:
#0 D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\library\Yii\web\CWebApplication.php(141): CWebApplication->runController('signinPartners...')
#1 D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\library\Yii\base\CApplication.php(183): CWebApplication->processRequest()
#2 D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\public\frontend\index.php(36): CApplication->run()
#3 {main}
REQUEST_URI=/signin
---
(the exception message is in french and means Unable to resolve the request « signin-partners/signin »)
Ok, big update, i've tried to play with routes.php and I realized that the name of my controller doesn't match with signinPartners. So, I update the routes rules with :
return array(
'' => 'index',
'signin' => 'signinPartner/signin',
);
And now, we have a new error : Controller can't find the view « /signin »..
There is the post Controller can't find the view in Yii which can anwser this question !
ANSWERED
try adding a - in signinPartners.
'signin-partners/signin'
The problem in my case was the action, the directory name in my view that contains the page and the route rule.
first, I update the route rule from :
return array(
'' => 'index',
'signin' => 'signinPartners/signin',
);
to :
return array(
'' => 'index',
'signin' => 'signinPartner/signin', // controlerName/actionName
);
The controller name was wrong.
In a second time, to match with the name of the controller, I simply renamed the directory in views calls signinPartners to signinPartner (only for readability).
Finally I update the action actionSignin that was :
public function actionSignin(){
$this->render('/signin');
}
For :
public function actionSignin(){
$this->render('/signinPartner/signin');
}
Where I change the path with the new directory name.
On my site I have this urlManager rule:
'city/<id:\d+>-<alias:\S*>' => 'city/view',
On page of module "user", for example this https://example.com/user/profile, there is a link for rule
Url::to(['city/view', 'id' => $this->id, 'alias' => $this->alias], $absolute)
But link becomes this https://example.com/user/city/view?id=1&alias=city_alias
What I am doing wrong?
You need to add module ID in the route as well. In the simplest case it's
'user/city/<id:\d+>-<alias:\S*>' => 'user/city/view'
If there are more than one module using similar route you can use wildcard
'<module>/city/<id:\d+>-<alias:\S*>' => '<module>/city/view'
You should simply try :
Url::to(['/city/view', 'id' => $this->id, 'alias' => $this->alias], $absolute)
Read more about creating urls.
I have following file structure
As default the url created for accessing the module content is for example
http://127.0.0.1/tmc/user/default/viewMessage
and for other controller it comes out to be
http://127.0.0.1/tmc/user/booking/index
The problem is I want to write a rule in my urlManager so that both controllers remain accessible AND i do not see default word in url as in first example.
However if i write following rules I am able to eliminate the default word but now other controllers in same module wont work. any help in this regard is appreciated
'<module:\w+>/<action:\w+>/<id:(.*?)>' => '<module>/default/<action>/<id>',
'<module:\w+>/<action:\w+>' => '<module>/default/<action>',
My current Url Manager is as follow
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'/' => 'site/index',
'login' => 'site/login',
'user' => 'user/default/',
'<view:[a-zA-Z0-9-]+>/' => 'site/page',
),
),
Follow this Link for config settings
Refer this Link
//inside protected/modules/admin/AdminModule.php
class AdminModule extends CWebModule
{
//goes to TaskController instead of DefaultController
public $defaultController = 'Task';
...
Now your Yii application will routes to the “TaskController” if you request
index.php?r=admin
//same as requesting
index.php?r=admin/task
I've got a set of routes that looks like this:
resources :placements do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
This produces routes that behave like you would expect:
/placements/1234/foo
/placements/1234/bar
However, I also need "generic" routes for a few methods that do not need an individual placement. So, I build a routes block that looks like this:
resources :placements do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
match '/placements/baz' => "placements#baz"
If I rake routes, I get a route that looks good:
/placements/baz
Note the lack of an id. However, if I try to visit that route, Rails tries to call the show method on the controller instead, as if "baz" was an ID, instead of a method name. How can I build a routing structure that gives me what I am after, without having to change the first segment of my route (placements), to something else?
Move the second route over the resources block ie,
match '/placements/baz' => "placements#baz"
resources :placements, :id => /\d+/ do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
or add a regex for the id in resources, ie something like:
resources :placements, :id => /\d+/ do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
match '/placements/baz' => "placements#baz"
I got website on Zend Framework (im total noob in Zend). For example I would like to make one URL "somewebsite.com/test/about" to look like this "somewebsite.com/for-fun-link". How do i achieve this in Zend ? Im newbie in Zend and Apache server. Where i do rewrites in Zend ? I want static URL somewebsite.com/page/about rewrite to somewebsite.com/about-product
And last question: where do i usually create rewrites ? its depends of sever/technology ?
In your bootstrap, you'll need to configure some "routes". So, if your bootstrap ends something like this:
$frontController = Zend_Controller_Front::getInstance();
$frontController->dispatch();
you can just add in some route definitions like this:
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute( 'mylovelyroute',
new Zend_Controller_Router_Route_Static( 'for-fun-link',
array( 'controller' => 'test', 'action' => 'about' )
)
);
$router->addRoute( 'myotherroute',
new Zend_Controller_Router_Route_Static( 'about-product',
array( 'controller' => 'page', 'action' => 'about' )
)
);
$router->addRoute( 'justonemore',
new Zend_Controller_Router_Route_Static( 'another/longer/path',
array( 'controller' => 'mycontroller',
'action' => 'myaction',
'someparameter' => 'foo'
)
)
);
$frontController->dispatch();
The first parameter of addRoute() is just a unique name. Zend_Controller_Router_Route_Static takes the path you'd like to capture, and then an array of parameters, including a controller and action (and module if it applies).
If you wanted to add complexity, you could load the routes out of a database, or start using more dynamic routes. The docs here are a useful next step: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard