Questions tagged [yii2-urlmanager]

Yii2 UrlManager handles HTTP request parsing and creation of URLs based on a set of rules. UrlManager is configured as an application component in yii\base\Application by default. You can access that instance via Yii::$app->urlManager. You can modify its configuration by adding an array to your application config under components.

Routing and URL Creation

When a Yii application starts processing a requested URL, the first step it takes is to parse the URL into a route. The route is then used to instantiate the corresponding controller action to handle the request. This whole process is called routing.

The reverse process of routing is called URL creation, which creates a URL from a given route and the associated query parameters. When the created URL is later requested, the routing process can resolve it back into the original route and query parameters.

The central piece responsible for routing and URL creation is the URL manager, which is registered as the urlManager application component. The URL manager provides the parseRequest() method to parse an incoming request into a route and the associated query parameters and the createUrl() method to create a URL from a given route and its associated query parameters.

By configuring the urlManager component in the application configuration, you can let your application recognize arbitrary URL formats without modifying your existing application code. For example, you can use the following code to create a URL for the post/view action:

use yii\helpers\Url;

// Url::to() calls UrlManager::createUrl() to create a URL
$url = Url::to(['post/view', 'id' => 100]);

Depending on the urlManager configuration, the created URL may look like one of the following (or other format). And if the created URL is requested later, it will still be parsed back into the original route and query parameter value.

/index.php?r=post%2Fview&id=100
/index.php/post/100
/posts/100
25 questions
1
vote
1 answer

How to transform url into the correct form in the Yii2 framework

This code $task->id]);?> transforms into the url web/tasks?view=1 what code and where will transform output variants into the web/tasks/view/1?
user17045604
1
vote
2 answers

Get full url of current webpage in Yii2

How to get the full URl in address bar to my Yii2 Model using yii\helpers\Url ? I use $currentUrl = Yii::$app->request->url; but it return site/submit which is defined already in my UrlManager : 'urlManager' => [ 'enableStrictParsing' =>…
Belicoff
  • 69
  • 1
  • 12
1
vote
1 answer

create dynamic url in yii2

my rules config is: [ 'pattern' => 'admin/post', 'route' => 'admin/default/post', ], it's work! when open http://example.com/admin/post show admin/default/post for me, but when i want create dynamic url with this code: echo…
Masoud92m
  • 602
  • 1
  • 8
  • 24
0
votes
0 answers

Yii2 UrlManager rules do not rewrite media files

I am using an app template based on the advanced template (frontend, common, backend). In the common's main.php (common/config/main.php) I have set a rule to map a particular url (ssid/) to site/index?ssid=. Here is the rule: 'urlManager' => [ …
axtho
  • 15
  • 6
0
votes
1 answer

Yii2 UrlManager rules

What would be the Urlmanager rule for changing a url like site/product?name=[name] to product/[name]? I tried '' => 'site/', 'product/' => 'product', But it gives me a 404
Andrew Sparrow
  • 167
  • 2
  • 2
  • 12
0
votes
0 answers

How to make my Yii2 urlManager Rule works? It returns 404 error

Please provide me some light! I have this existing rule 'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => false, 'showScriptName' => false, 'rules' => [ [ …
lordrebs
  • 11
  • 2
0
votes
1 answer

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…
idunno
  • 71
  • 8
0
votes
1 answer

URL Suffix Management with YII2 is causing error

I am facing an issue in my YII2 project. I am adding the URL suffix in the rules and it is throwing me 404 error. I don't know why i am facing this issue. Let me share my url Manager rule 'urlManager' => [ 'enablePrettyUrl' => true, …
kool_Coder
  • 149
  • 1
  • 12
0
votes
1 answer

Yii2 - Remove default module controller from URL

I have the following rules in my urlManager: [ 'urlManager' => [ …
Alejandro
  • 809
  • 1
  • 10
  • 21
0
votes
0 answers

Yii2 pretty urls returning 404

I am using nginx and yii2 trying to create a little sample crud rest API, and whenever I enable pretty urls I can't access my routes anmymore. My config is as follows : 'urlManager' => [ 'enablePrettyUrl' => true, …
Weev
  • 36
  • 1
  • 8
0
votes
1 answer

Yii2 url route manager rules fails for multiple paths

I would like have all routes except the api routes to navigate to the site/index route but all /api path to be executed to the respective modules. I have added the following route rules 'urlManager' => [ 'enablePrettyUrl' => true, …
Geoff
  • 6,277
  • 23
  • 87
  • 197
0
votes
1 answer

Yii2 urlmanager rules for pretty urls fails

I have the following url manager path 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '/' => 'site/index', …
Geoff
  • 6,277
  • 23
  • 87
  • 197
0
votes
1 answer

Yii2: How to redirect on invalid routes?

Instead of showing an error page I'd like to redirect the user to the start page in general, if the route is invalid (there is no controller/action like requested). To make it sound the redirection should only happen on 'normal' page requests.…
robsch
  • 9,358
  • 9
  • 63
  • 104
0
votes
0 answers

yii2 UrlManager error with last element in url

How can I set up urlManager rules like that? /category/category1/element/ /category2/element/ Yii perceives the last element as a category. I need exactly this scheme url. If you do not put the last slash, then everything is OK, but it is…
Mary
  • 37
  • 9
0
votes
0 answers

yii2 conflict in Urlmanager

Help me find a solution for subsections. Category: /blog/cat1/ /blog/cat/cat2/cat3/ Detail: /blog/cat/cat2/cat3/element_code Subsection for tags in section 'blog': /blog/tag/ /blog/tag/some-tag/ I have this rules: [ 'pattern' =>…
Mary
  • 37
  • 9
1
2