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
6a1816a9
Commit
6a1816a9
authored
Feb 20, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow using string for URL in case there are no parameters
parent
43c17d99
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
23 additions
and
18 deletions
+23
-18
url.md
docs/guide/url.md
+6
-6
DefaultController.php
extensions/gii/controllers/DefaultController.php
+2
-1
Generator.php
extensions/gii/generators/controller/Generator.php
+2
-2
Generator.php
extensions/gii/generators/module/Generator.php
+1
-1
Controller.php
framework/web/Controller.php
+6
-4
UrlManager.php
framework/web/UrlManager.php
+6
-4
No files found.
docs/guide/url.md
View file @
6a1816a9
...
...
@@ -37,7 +37,7 @@ echo \Yii::$app->urlManager->createUrl(['site/page', 'id' => 'about']);
// /index.php/site/page/id/about/
echo
\Yii
::
$app
->
urlManager
->
createUrl
([
'date-time/fast-forward'
,
'id'
=>
105
])
// /index.php?r=date-time/fast-forward&id=105
echo
\Yii
::
$app
->
urlManager
->
createAbsoluteUrl
(
[
'blog/post/index'
]
);
echo
\Yii
::
$app
->
urlManager
->
createAbsoluteUrl
(
'blog/post/index'
);
// http://www.example.com/index.php/blog/post/index/
```
...
...
@@ -57,9 +57,9 @@ Inside a web application controller, you can use the controller's `createUrl` sh
```
php
echo
$this
->
createUrl
(
''
);
// currently active route
echo
$this
->
createUrl
([
'view'
,
'id'
=>
'contact'
]);
// same controller, different action
echo
$this
->
createUrl
(
[
'post/index'
]
);
// same module, different controller and action
echo
$this
->
createUrl
(
[
'/site/index'
]
);
// absolute route no matter what controller is making this call
echo
$this
->
createurl
(
[
'hi-tech'
]
);
// url for the case sensitive action `actionHiTech` of the current controller
echo
$this
->
createUrl
(
'post/index'
);
// same module, different controller and action
echo
$this
->
createUrl
(
'/site/index'
);
// absolute route no matter what controller is making this call
echo
$this
->
createurl
(
'hi-tech'
);
// url for the case sensitive action `actionHiTech` of the current controller
echo
$this
->
createurl
([
'/date-time/fast-forward'
,
'id'
=>
105
]);
// url for action the case sensitive controller, `DateTimeController::actionFastForward`
```
...
...
@@ -114,11 +114,11 @@ Let's use some examples to explain how URL rules work. We assume that our rule s
]
```
-
Calling
`$this->createUrl(
['post/list']
)`
generates
`/index.php/posts`
. The first rule is applied.
-
Calling
`$this->createUrl(
'post/list'
)`
generates
`/index.php/posts`
. The first rule is applied.
-
Calling
`$this->createUrl(['post/read', 'id' => 100])`
generates
`/index.php/post/100`
. The second rule is applied.
-
Calling
`$this->createUrl(['post/read', 'year' => 2008, 'title' => 'a sample post'])`
generates
`/index.php/post/2008/a%20sample%20post`
. The third rule is applied.
-
Calling
`$this->createUrl(
['post/read']
)`
generates
`/index.php/post/read`
. None of the rules is applied, convention is used
-
Calling
`$this->createUrl(
'post/read'
)`
generates
`/index.php/post/read`
. None of the rules is applied, convention is used
instead.
In summary, when using
`createUrl`
to generate a URL, the route and the
`GET`
parameters passed to the method are used to
...
...
extensions/gii/controllers/DefaultController.php
View file @
6a1816a9
...
...
@@ -110,8 +110,9 @@ class DefaultController extends Controller
/**
* @inheritdoc
*/
public
function
createUrl
(
array
$params
)
public
function
createUrl
(
$params
)
{
$params
=
(
array
)
$params
;
if
(
!
isset
(
$params
[
'id'
])
&&
$this
->
generator
!==
null
)
{
foreach
(
$this
->
module
->
generators
as
$id
=>
$generator
)
{
if
(
$generator
===
$this
->
generator
)
{
...
...
extensions/gii/generators/controller/Generator.php
View file @
6a1816a9
...
...
@@ -149,9 +149,9 @@ class Generator extends \yii\gii\Generator
{
$actions
=
$this
->
getActionIDs
();
if
(
in_array
(
'index'
,
$actions
))
{
$route
=
[
$this
->
controller
.
'/index'
]
;
$route
=
$this
->
controller
.
'/index'
;
}
else
{
$route
=
[
$this
->
controller
.
'/'
.
reset
(
$actions
)]
;
$route
=
$this
->
controller
.
'/'
.
reset
(
$actions
)
;
}
$link
=
Html
::
a
(
'try it now'
,
Yii
::
$app
->
getUrlManager
()
->
createUrl
(
$route
),
[
'target'
=>
'_blank'
]);
return
"The controller has been generated successfully. You may
$link
."
;
...
...
extensions/gii/generators/module/Generator.php
View file @
6a1816a9
...
...
@@ -84,7 +84,7 @@ class Generator extends \yii\gii\Generator
public
function
successMessage
()
{
if
(
Yii
::
$app
->
hasModule
(
$this
->
moduleID
))
{
$link
=
Html
::
a
(
'try it now'
,
Yii
::
$app
->
getUrlManager
()
->
createUrl
(
[
$this
->
moduleID
]
),
[
'target'
=>
'_blank'
]);
$link
=
Html
::
a
(
'try it now'
,
Yii
::
$app
->
getUrlManager
()
->
createUrl
(
$this
->
moduleID
),
[
'target'
=>
'_blank'
]);
return
"The module has been generated successfully. You may
$link
."
;
}
...
...
framework/web/Controller.php
View file @
6a1816a9
...
...
@@ -159,11 +159,12 @@ class Controller extends \yii\base\Controller
*
* After this route conversion, the method calls [[UrlManager::createUrl()]] to create a URL.
*
* @param
array $params
route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param
string|array $params route as a string or
route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @return string the created relative URL
*/
public
function
createUrl
(
array
$params
)
public
function
createUrl
(
$params
)
{
$params
=
(
array
)
$params
;
$params
[
0
]
=
$this
->
getNormalizedRoute
(
$params
[
0
]);
return
Yii
::
$app
->
getUrlManager
()
->
createUrl
(
$params
);
}
...
...
@@ -182,13 +183,14 @@ class Controller extends \yii\base\Controller
*
* After this route conversion, the method calls [[UrlManager::createUrl()]] to create a URL.
*
* @param
array $params
route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param
string|array $params route as a string or
route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param string $schema the schema to use for the url. e.g. 'http' or 'https'. If not specified
* the schema of the current request will be used.
* @return string the created absolute URL
*/
public
function
createAbsoluteUrl
(
array
$params
,
$schema
=
null
)
public
function
createAbsoluteUrl
(
$params
,
$schema
=
null
)
{
$params
=
(
array
)
$params
;
$params
[
0
]
=
$this
->
getNormalizedRoute
(
$params
[
0
]);
return
Yii
::
$app
->
getUrlManager
()
->
createAbsoluteUrl
(
$params
,
$schema
);
}
...
...
framework/web/UrlManager.php
View file @
6a1816a9
...
...
@@ -228,11 +228,12 @@ class UrlManager extends Component
/**
* Creates a URL using the given route and parameters.
* The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL.
* @param
array $params
route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param
string|array $params route as a string or
route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @return string the created URL
*/
public
function
createUrl
(
array
$params
)
public
function
createUrl
(
$params
)
{
$params
=
(
array
)
$params
;
$anchor
=
isset
(
$params
[
'#'
])
?
'#'
.
$params
[
'#'
]
:
''
;
unset
(
$params
[
'#'
],
$params
[
$this
->
routeParam
]);
...
...
@@ -275,14 +276,15 @@ class UrlManager extends Component
/**
* Creates an absolute URL using the given route and parameters.
* This method prepends the URL created by [[createUrl()]] with the [[hostInfo]].
* @param
array $params
route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param
string|array $params route as a string or
route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param string $schema the schema to use for the url. e.g. 'http' or 'https'. If not specified
* the schema of the current request will be used.
* @return string the created URL
* @see createUrl()
*/
public
function
createAbsoluteUrl
(
array
$params
,
$schema
=
null
)
public
function
createAbsoluteUrl
(
$params
,
$schema
=
null
)
{
$params
=
(
array
)
$params
;
$url
=
$this
->
createUrl
(
$params
);
if
(
strpos
(
$url
,
'://'
)
===
false
)
{
$url
=
$this
->
getHostInfo
(
$schema
)
.
$url
;
...
...
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