Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
1d3b3183
Commit
1d3b3183
authored
May 13, 2011
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added converters for request attributes
parent
2ae7fdf4
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
96 additions
and
0 deletions
+96
-0
doc/usage.rst
doc/usage.rst
+35
-0
src/Silex/Application.php
src/Silex/Application.php
+18
-0
src/Silex/Controller.php
src/Silex/Controller.php
+15
-0
tests/Silex/Tests/ApplicationTest.php
tests/Silex/Tests/ApplicationTest.php
+19
-0
tests/Silex/Tests/ControllerTest.php
tests/Silex/Tests/ControllerTest.php
+9
-0
No files found.
doc/usage.rst
View file @
1d3b3183
...
...
@@ -228,6 +228,41 @@ While it's not suggested, you could also do this (note the switched arguments)::
...
});
Route variables converters
~~~~~~~~~~~~~~~~~~~~~~~~~~
Before injecting the route variables into the controller, you can apply some
converters::
$app->get('/user/{id}', function ($id) {
// ...
})->convert('id', function ($id) { return (int) $id; });
This is useful when you want to convert route variables to objects as it
allows to reuse the conversion code across different controllers::
$userProvider = function ($id) {
return new User($id);
};
$app->get('/user/{user}', function (User $user) {
// ...
})->convert('user', $userProvider);
$app->get('/user/{user}/edit', function (User $user) {
// ...
})->convert('user', $userProvider);
The converter callback also receives the ``Request`` as its second argument::
$callback = function ($post, Request $request) {
return new Post($request->attributes->get('slug'));
};
$app->get('/blog/{id}/{slug}', function (Post $post) {
// ...
})->convert('post', $callback);
Requirements
~~~~~~~~~~~~
...
...
src/Silex/Application.php
View file @
1d3b3183
...
...
@@ -17,6 +17,7 @@ use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use
Symfony\Component\HttpKernel\Event\KernelEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
;
use
Symfony\Component\HttpKernel\Event\FilterControllerEvent
;
use
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
;
use
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
;
use
Symfony\Component\HttpKernel\Events
as
HttpKernelEvents
;
...
...
@@ -347,6 +348,22 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this
[
'dispatcher'
]
->
dispatch
(
Events
::
onSilexBefore
);
}
/**
* Handles converters.
*
* @param FilterControllerEvent $event A FilterControllerEvent instance
*/
public
function
onCoreController
(
FilterControllerEvent
$event
)
{
$request
=
$event
->
getRequest
();
$route
=
$this
[
'routes'
]
->
get
(
$request
->
attributes
->
get
(
'_route'
));
if
(
$route
&&
$converters
=
$route
->
getOption
(
'_converters'
))
{
foreach
(
$converters
as
$name
=>
$callback
)
{
$request
->
attributes
->
set
(
$name
,
call_user_func
(
$callback
,
$request
->
attributes
->
get
(
$name
,
null
),
$request
));
}
}
}
/**
* Handles string responses.
*
...
...
@@ -396,6 +413,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
return
array
(
HttpKernelEvents
::
onCoreRequest
,
HttpKernelEvents
::
onCoreController
,
HttpKernelEvents
::
onCoreResponse
,
HttpKernelEvents
::
onCoreException
,
);
...
...
src/Silex/Controller.php
View file @
1d3b3183
...
...
@@ -95,6 +95,21 @@ class Controller
return
$this
;
}
/**
* Sets a converter for a route variable.
*
* @param string $variable The variable name
* @param mixed $callback A PHP callback that converts the original value
*/
public
function
convert
(
$variable
,
$callback
)
{
$converters
=
$this
->
route
->
getOption
(
'_converters'
);
$converters
[
$variable
]
=
$callback
;
$this
->
route
->
setOption
(
'_converters'
,
$converters
);
return
$this
;
}
/**
* Freezes the controller.
*
...
...
tests/Silex/Tests/ApplicationTest.php
View file @
1d3b3183
...
...
@@ -85,6 +85,25 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
2
,
count
(
$routes
->
all
()));
}
public
function
testOnCoreController
()
{
$app
=
new
Application
();
$app
->
get
(
'/foo/{foo}'
,
function
(
\ArrayObject
$foo
)
{
return
$foo
[
'foo'
];
})
->
convert
(
'foo'
,
function
(
$foo
)
{
return
new
\ArrayObject
(
array
(
'foo'
=>
$foo
));
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo/bar'
));
$this
->
assertEquals
(
'bar'
,
$response
->
getContent
());
$app
->
get
(
'/foo/{foo}/{bar}'
,
function
(
\ArrayObject
$foo
)
{
return
$foo
[
'foo'
];
})
->
convert
(
'foo'
,
function
(
$foo
,
Request
$request
)
{
return
new
\ArrayObject
(
array
(
'foo'
=>
$foo
.
$request
->
attributes
->
get
(
'bar'
)));
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo/foo/bar'
));
$this
->
assertEquals
(
'foobar'
,
$response
->
getContent
());
}
/**
* @dataProvider escapeProvider
*/
...
...
tests/Silex/Tests/ControllerTest.php
View file @
1d3b3183
...
...
@@ -60,6 +60,15 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
array
(
'bar'
=>
'foo'
),
$controller
->
getRoute
()
->
getDefaults
());
}
public
function
testConvert
()
{
$controller
=
new
Controller
(
new
Route
(
'/foo/{bar}'
));
$ret
=
$controller
->
convert
(
'bar'
,
$func
=
function
(
$bar
)
{
return
$bar
;
});
$this
->
assertSame
(
$ret
,
$controller
);
$this
->
assertEquals
(
array
(
'bar'
=>
$func
),
$controller
->
getRoute
()
->
getOption
(
'_converters'
));
}
/**
* @dataProvider provideRouteAndExpectedRouteName
*/
...
...
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