Remove controller name from URLs in Yii2 - php

How can I remove the controller name from URLs in Yii2?
I'm aware of simple aliases, but I'm not sure how to apply aliases to a little bit more complex rule, like the following one:
'rules' => [
'public/<seo_url:.*?>/<category_id:\d+>/<product_id:\d+>' => 'public/product'
]
This will create a rule so URLs like /product/this-is-a-slug/94/12 will call the actionProduct in my Public controller.
How can I make an alias that would call that same action if I try to access a product using this URL: /this-is-a-slug/94/12 ?

Set new rulw on top of all rules. Like that:
'rules' => [
'<seo_url:.*?>/<category_id:\d+>/<product_id:\d+>' => 'public/product',
]
This is work for me.

Related

codeigniter 4 filters with except

Currently experimenting with Codeigniter 4 Filter, for an admin section.
On $filters am I able to except a function like this:
public $filters = [
'authenticate' => [
'before' => [
'admin/*',
],
'except' => [
'admin/verify'
],
],
];
So it basically add filter 'authenticate' to every admin controller except the verify one.
Doesn't seem to work.
Also wanted to use arguments on this $filters var like this:
public $filters = [
'authenticate:admin' => [
Which also fail to work.
Am i able to make it work on Routes, but then I need to specify all the controllers functions for the filter to work. Also arguments work on the Routes.
Thanks
The docs mention this very badly positioned:
Any place you can use a URI in the filter settings, you can use a regular expression or, like in this example, use an asterisk for a wildcard that will match all characters after that.
So you can just use a normal regular expression to achieve your goal. For example if you want all routes for user/* pass the authentication filter, BUT not user/settings, this is the way to go:
public $filters = [
"authentication" => [
"before" => [
"/user\/(?!settings)/",
]
]
];

react with yii urlManager rule to a specific url

I have a noob question, but googling for several hours didn't get me closer to the answer.
I want that when I enter the following url into my browser, that is gets assigned to a specific controller action.
Imagine I have the following url:
http://localhost:5555/tractor-unit/?param1=1&param2=abc
How should the rules looks like to process this request in a certain action in a certain controller.
What I've tried so far:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'tractor-unit/?param1=<param1:\d+>&param2=<param2:\w+>' => 'tractor/do'
]
],
That doesn't work. I just don't understand how the rules entry should be written in order to pass this request to a controller named tractor and its action do. Or maybe I totally misunderstood the whole url manager concept.
simple add to action "do" in tractor-unit controller next:
public function actionDo($param1=null, $param2 = null)
if this don't work
remove section "rules" from config

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'
],
],

Use Upper-Case letter for the first and second characters of controller name Yii2 [duplicate]

This question already has answers here:
Yii2 routing when using CamelCase action names
(2 answers)
Closed 5 years ago.
I am using Yii2 framework and based on some standards I have to customize my controller name.
As you know the standard for controller name in yii2 is like this:
If you name your controller like this :DServicesController, The route in pretty url for accessing this controller's actions is /d-services/[actionName].
But I want to name my controller in a way that I can access it like this in the url:
/DServices/[action name]
I added the following code to my urlManager rules but didn't work:
'urlManager' => [
'rules' => [
'manage/DServices/*' => 'manage/d-services/<action>'
],
]
How can I name my controller class or define specific rule to access it like this.
P.S: As Yii2 standard the first letter of your controller name always changes to Lower-Case.
Actually upper case letters works fine in Yii2 routing rules. But I see two strange things in Your snippet:
Asterisk in route template. Did You want to pass parameters this way?
manage/ prefix in action route. It will work if your controller is in manage module.
I tried to use Your template with these things fixed:
'urlManager' => [
'rules' => [
'manage/DServices' => 'd-services/<action>'
],
]
and it works as expected.
See this section of Yii2 docs for details: http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#routing

Categories