Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
c3e4d636
Commit
c3e4d636
authored
Nov 30, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Docs on creating your own rule classes
parent
bcff068b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
2 deletions
+68
-2
url.md
docs/guide/url.md
+68
-2
No files found.
docs/guide/url.md
View file @
c3e4d636
...
...
@@ -121,4 +121,71 @@ return [
Creating your own rule classes
------------------------------
TBD
\ No newline at end of file
[
[\yii\web\UrlRule
]
] class is used for both parsing URL into parameters and creating URL based on parameters. Despite
the fact that default implementation is flexible enough for majority of projects, there could be a situation when using
your own rule class is the best choice. For example, in a car dealer website, we may want to support the URL format like
`/Manufacturer/Model`
, where
`Manufacturer`
and
`Model`
must both match some data in a database table. The default rule
class will not work because it mostly relies on statically declared regular expressions which have no database knowledge.
We can write a new URL rule class by extending from
[
[\yii\web\UrlRule
]
] and use it in one or multiple URL rules. Using
the above car dealer website as an example, we may declare the following URL rules in application config:
```
php
// ...
'components'
=>
[
'urlManager'
=>
[
'rules'
=>
[
'<action:(login|logout|about)>'
=>
'site/<action>'
,
// ...
[
'class'
=>
'app\components\CarUrlRule'
,
'connectionID'
=>
'db'
,
...
],
],
],
],
```
In the above, we use the custom URL rule class
`CarUrlRule`
to handle
the URL format
`/Manufacturer/Model`
. The class can be written like the following:
```
php
namespace
\app\components
;
use
\yii\web\UrlRule
;
class
CarUrlRule
extends
UrlRule
{
public
$connectionID
=
'db'
;
public
function
createUrl
(
$manager
,
$route
,
$params
)
{
if
(
$route
===
'car/index'
)
{
if
(
isset
(
$params
[
'manufacturer'
],
$params
[
'model'
]))
{
return
$params
[
'manufacturer'
]
.
'/'
.
$params
[
'model'
];
}
elseif
(
isset
(
$params
[
'manufacturer'
]))
{
return
$params
[
'manufacturer'
];
}
}
return
false
;
// this rule does not apply
}
public
function
parseRequest
(
$manager
,
$request
)
{
$pathInfo
=
$request
->
getPathInfo
();
if
(
preg_match
(
'%^(\w+)(/(\w+))?$%'
,
$pathInfo
,
$matches
))
{
// check $matches[1] and $matches[3] to see
// if they match a manufacturer and a model in the database
// If so, set $_GET['manufacturer'] and/or $_GET['model']
// and return 'car/index'
}
return
false
;
// this rule does not apply
}
}
```
Besides the above usage, custom URL rule classes can also be implemented
for many other purposes. For example, we can write a rule class to log the URL parsing
and creation requests. This may be useful during development stage. We can also
write a rule class to display a special 404 error page in case all other URL rules fail
to resolve the current request. Note that in this case, the rule of this special class
must be declared as the last rule.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment