How to configure Yii's UrlManager for OpenSearch controller - php

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.

Related

How can I use dash with multiple words in Yii2 page creation

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.

Yii2 Create Pretty URL like /index.php/viewName/actionName/var1/value1/var2/value2

i am playing around with yii2 (recently been working in Yii 1.3) and need help to configure/write the Url-Manager Rules for my favorite URL-sheme.
As Example, i wanna call the action test from xmpleController with 2 parameters.
a normal GET request would looks like this:
?param1=value1&param2=value2
at the moment, my url look like this:
index.php/xmple/test/?param1=value1&param2=value2
This is how the url should look like:
index.php/xmple/test/param1/value1/param2/value2
here are my URL-Manager Rules:
'urlManager' => [
'enablePrettyUrl' => True,
'showScriptName' => false,
'rules' => [
'<a:\w+>/<b:\w+>/<c:\d+>/<d:\d+>' => 'a/b'
],
],
Does anybody have an Idea how I can use my favorite URL scheme? I think the only way to reach my goal is to edit the urlManager rule, but I don't have any experience in this. maybe someone here has some hint for me?
Thanks for your help!
Before you start making your desired URL format you need to understand first what formats are supported by the URL manager when working in Yii2. And how to create rules to create those formats.
Supported URL Formats
The default URL format;
The default URL format uses a query parameter named r to represent the route and normal query parameters to represent the query parameters associated with the route. The URL /index.php?r=xmple/test&param1=100 represents the route xmple/test and the param1 query parameter 100. The default URL format does not require any configuration of the URL manager and works in any Web server setup.
The pretty URL format.
Uses the extra path following the entry script name to represent the route and the associated query parameters. For example, the extra path in the URL /index.php/xmple/100 is /xmple/100 which may represent the route xmple/test and the param1 query parameter 100 with a proper URL rule. To use the pretty URL format, you will need to design a set of URL rules according to the actual requirement about how the URLs should look like.
This rule can satisfy the above statement 'xmple/<param1:\d+>' => 'xmple/test',
Read More about it here
So it is not going to be showing
index.php/xmple/test/param1/value1/param2/value2 but
index.php/xmple/test/value1/value2 or index.php/xmple/value1/value2 or xmple/test/value1/value2.
How to create Rules
You can configure yii\web\UrlManager::$rules as an array with keys being the patterns and values the corresponding routes.
Read More here
You can use the rule 'xmple/test/<param1:\w+>/<param2:\w+>'=>'xmple/test' considering that you will be sending parameters that match any word character (equal to [a-zA-Z0-9_]) as a parameter, which will output xmple/test/value1/value2 with 'showScriptName' => false, or index.php/xmple/test/value1/value2 otherwise.
If the rule is going to be used for a single controller/action or you can use as described or use the parameterized routes which allows a URL rule to be used for matching multiple routes.
You can change your urlManager to the following
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'xmple/test/<param1:\w+>/<param2:\w+>'=>'xmple/test'
],
],

Yii2 does not find action

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

Yii2 Url Mapping Suffix

i have some simular Url Mapping in Yii2:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
['pattern'=>'pages','route'=>'pages/index', 'suffix'=>'/'],
['pattern'=>'pages/<id:\d+>','route'=>'pages/single', 'suffix'=>'.html'],
]]
So, urls:
domain.com/pages/ - works fine
domain.com/pages - E404
domain.com/pages/321.html - works fine
domain.com/pages/321 - E404
If i remove suffix parameter from config array, then:
domain.com/pages/ - E404
domain.com/pages - works fine
domain.com/pages/321.html - E404
domain.com/pages/321 - works fine
Is it possible to make 'suffix' into link optional, e.g. 'pages' and 'pages/' will be followed to one direction?
Thanks.
Seems like it's not possible to achieve this with rules configuration in url manager.
Please check these related issues on Github:
https://github.com/yiisoft/yii2/issues/1807
https://github.com/yiisoft/yii2/issues/6498
So choose one option that more suitable for you and use it.
Need trailing slash or .html - add suffix, otherwise just leave that as is.
Also for search engines
domain.com/pages/
domain.com/pages
these are the two different pages and using that will entail content duplicates.

Yii url management issue

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.

Categories