'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>/<action:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
Not it is clear why references of a /controller/name-action/id/1 do work, /controller/1/name-action do not work, and without hyphen everything works, according to documentation of action name-action it is actionNameAction??
public function actionNameAction($id) {
// some code
}
Inline Actions
In advance all thanks.
\w does not include -.
Change pattern to [\w\-]+.
I think you are confused with zend framework and Yii2.In yii2 it is actionActionName.While using it in url's the capitals are changed to lower case with a hyphen preceding them.
For example,
if the controller is Orders
and the action is OrderAnalysis
then the url would be something like orders/order-analysis.
Moreover any id or any other parameter is added only after routing the app to its correct controller action.
Also now coming to your problem I think i found a backdoor to it-
// creates an anchored URL: /index.php?r=post%2Fview&id=100#content --------------------------
echo Url::to(['post/view', 'id' => 100, '#' => 'content']);
Related
I have the following url manager path
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:(\w|-)+>/' => 'site/index',
'<module:api\w+>/<controller:\w+>/<action:(\w|-)+>' => '<module>/<controller>/<action>',
],
]
What am looking for is all urls not rendered via api module paths to run via site/index but all other paths having api/* to be executed via module paths.
The above works for urls like /login, /auth but when i run urls like
/administrative/uom
It fails
SO basically i want all urls to be redirrected via site/index but all urls having api as the prefix like api/auth/login to be run via their respective controllers.
I have added an api module which should handle this.
What else do i need to add to make this work?
When adding rules always start from more detailed to less detailed. And you added general rule for controller only so no URL with action is matched. Do this
'rules' => [
'api/<controller:\w+>/<action:[\w\-]+>' => 'api/<controller>/<action>',
'<controller:[\w\-]+>/<action:[\w\-]+>' => 'site/index',
'<controller:[\w\-]+>/' => 'site/index',
],
I am trying to create a static page with Yii2. The page name has more has two words for example - privacy policy. I added the code below inside SiteController.php
public function actionPrivacyPolicy()
{
return $this->render('privacy-policy');
}
I then created a page inside the site folder named - privacy-policy.php
This however resulted in page not found error.
My website has many cases like this. What is the best way to fix this?
You need to access the url like below if using "enablePrettyUrl"=>true
http://yoursite.com/site/privacy-policy
and
http://yoursite.com/index.php?r=site/privacy-policy
if not using "enablePrettyUrl"=>true
after that you need to parse all the actions in SiteController by the action names only, so you need to turn "enableStrictParsing"=>false and then add the following rule in the urlManager
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
"enableStrictParsing" => false,
'rules' => [
"/" => "site/index",
"<action:(.*)>" => 'site/<action>',
],
],
EDIT:
Although this allows characters that are/should not be allowed but, The reason I have used (.*) in the rules is because the action name will be defined inside the controllers and you have to use the rules with the declared actions only inside the SiteController which isn't going to parse any action name for rules with any of the disallowed characters, means if the action name is going to be, say actionMyActionNameLong there is 0 possibility that any / or \ is generated with the action name which would break the things further when using the rule, so you can use it in this context otherwise you can use the [\w\-]+ if creating the urls manually.
Pattern <alias:\w+> will not match hyphen, since \w+ will accept only letters, numbers and underscore. You need to use [\w\-]+ to match also words separated by hyphen.
'rules' => [
'<alias:[\w\-]+>' => 'site/<alias>',
// ...
],
I think you can configure UrlManager in main.php on the config folder
and try to add some rules on urlManager so the URL can be accessed dynamically
'urlManager' => [
"enablePrettyUrl" => true,
"showScriptName" => false,
"enableStrictParsing" => false,
"suffix" => "",
"rules" => [
"<controller:\w+>/<action:\w+>" => "<controller>/<action>"
],
],
If you are using \yii\filters\AccessControl on your controllers behaviour, you might need to add allowed action to the list before creating a new action.
Instead of have:
http://www.example.com/affiliate?ref=123456546546
I prefer to have
http://www.example.com/affiliate/ref/123456546546
Is it possible to pass GET param without changing .htaccess and keep my desired structure ?
try to add an rule to your UrlManager I guess your controller is affiliate and your action index
'urlManager' => [
...
'rules' => [
'<controller:(affiliate)>/ref/<ref:\d+>' => '<controller>/index',
]
...
]
I guess if you set custom rules you also have to add the following so that the default url's are still working.
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
I'm developing a web application using Yii Framework and I'm having a little problem with url rewriting.
The issue is with the last rule in the code below. It was supposed to turn localhost/app/category/some-friendly-url-category-name (this part would appear in the browser) into localhost/app/category/view/id/some-friendly-url-category-name
It works fine when the category name is only one word, like: vehicles.
But it doesn't work when it has more than a word, like: children-stuff
'urlManager'=>array(
'showScriptName' => false,
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'category/<id:[A-Za-z0-9_\-]+>' => 'category/view',
),
),
You don't need to escape the hyphen in the ID:
'category/<id:[A-Z a-z 0-9 _ -]+>' => 'category/view',
A simpler solution would be:
'category/<id>' => 'category/view',
Also, do note that you have to place this specific rule above the general (default) rules.
Move the category rule above the <controller> ones. Otherwise, your app would try to find a actionChildrenStuff() in your CategoryController, for example.
Usually I would add new rules above those three general ones.
I am trying to add OpenSearch functionality to my Yii application and have trouble configuring the UrlManager to route a search request to the right controller (called query in this case) and method (called index). The search works great if there are no white space in the search words. Whenever someone searches for more than one word, the UrlManager cannot find the controller and method to handle the search.
The url in the search.xml file that triggers the search is like this:
<Url type="text/html" template="[pathToMyApp]/application/index.php/query/{searchTerms}"/>
And my UrlManager is configured like this:
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'<_c:(name|tag)>s/*' => '<_c>/index',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'query/suggest/<needle:\w+>'=>'query/suggest', // used to route OpenSearch JSON suggestions - works
'query/findbox'=>'query/findbox', // routes search queries from a form in the application - works
'query/<needle:\w+>'=>'query/index', // works only for search strings with no white space
),
'showScriptName' => true,
),
It seems that in query/<needle:\w+>, the \w+ part will stop matching at the first non-word char (e.g. a space). Changing the \w to something more forgiving (e.g. [\w ]) will probably do the trick.