Symfony2 has basic rule for each user request as
take user request -> process request -> generate response .
Routing system act as middle man for request and response. it accept the request then identify the controller then identify the appropriate action method.
for example user request for http:{host_name}/demo/test
inside routing system you can build the pattern to match the request as
demo_test:
pattern: /demo/test
defaults: { _controller: DemoBundle:Demo:test }
here routing identify the bundle as "DemoBundle" then move to "Demo" controller and execute "test" action.
you can also add parameter to to routing system as
demo_test:
pattern: /demo/test/{page}
defaults: { _controller: DemoBundle:Demo:test }
here page value become parameter inside the action like
public function testAction($page)
here page parameter is required parameter, it must be present in the request URI otherwise routing system looks for other routing path.
to make parameter optional add this line in routing system
demo_test:
pattern: /demo/test/{page}
defaults: { _controller: DemoBundle:Demo:test , page : 1}
if page parameter is not available then default page value as 1 is added to $page parameter.
you can pass parameter to action without defining it in routing system. each request parameter in present in request object which can be access inside the action.
Note: routing system is used to design user friendly URI. it is not necessary to add parameter to each routing system. each parameters are available to action by request object.
take user request -> process request -> generate response .
Routing system act as middle man for request and response. it accept the request then identify the controller then identify the appropriate action method.
for example user request for http:{host_name}/demo/test
inside routing system you can build the pattern to match the request as
demo_test:
pattern: /demo/test
defaults: { _controller: DemoBundle:Demo:test }
here routing identify the bundle as "DemoBundle" then move to "Demo" controller and execute "test" action.
you can also add parameter to to routing system as
demo_test:
pattern: /demo/test/{page}
defaults: { _controller: DemoBundle:Demo:test }
here page value become parameter inside the action like
public function testAction($page)
here page parameter is required parameter, it must be present in the request URI otherwise routing system looks for other routing path.
to make parameter optional add this line in routing system
demo_test:
pattern: /demo/test/{page}
defaults: { _controller: DemoBundle:Demo:test , page : 1}
if page parameter is not available then default page value as 1 is added to $page parameter.
you can pass parameter to action without defining it in routing system. each request parameter in present in request object which can be access inside the action.
Note: routing system is used to design user friendly URI. it is not necessary to add parameter to each routing system. each parameters are available to action by request object.
No comments:
Post a Comment