Professional Documents
Culture Documents
Routing en
Routing en
Symfony 2.2
All routes are loaded via a single configuration file - usually app/config/routing.yml
You can change this through the app/config/config.yml file:
Prefixing Imported Routes
You can also choose to provide a "prefix" for the imported routes
framework:
# ... # app/config/routing.yml
router: { resource: "%kernel.root_dir%/config/routing.yml" } acme_hello:
YAML
resource: "@AcmeHelloBundle/Resources/config/routing.yml"
Defining your routes prefix: /admin
The string /admin will now be prepended to the pattern of
MyBundle/Resources/config - you can create your routes here and import
each route loaded from the new routing resource.
the file in the app/config/routing.yml:
YAML
acme_hello:
A @Route annotation on a controller class defines a prefix for all
resource: "@AcmeHelloBundle/Resources/config/routing.yml" action routes:
You can also create several route files in MyBundle/Resources/config/routing /**
and import them in your MyBundle/Resources/config/routing.yml * @Route("/blog")
Annotation
*/
class PostController extends Controller{
Annotation
To use route annotation, you have to import routes from a controller class:
/**
# app/config/routing.yml * @Route("/{id}")
post: */
resource: "@SensioBlogBundle/Controller/BlogController.php"
type: annotation public function showAction($id){
}
}
How?
The route below match: http://www.myurl.com/blog/en/2 Placeholders:
Required by default.
Name of the route Set a default value in this example:
blog: to make it optional culture - required
Pattern to match path: /blog/{culture}/{page} page - optional
older versions:
pattern defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 } Each routing parameter
requirements: or default value is available
Regexp requirement page: \d+ Controller to be executed as an argument in the
(only digits) to the (format: Bundle:Controller:method) controller method.
culture: en|pt
page parameter
YAML
methods: [GET]
Requirement to the culture
older versions: This route accepts parameter accepts only the
_method only the GET method en and pt values
Adding a Host
Special Routing Parameters user_homepage: http://jack.myurl.com/
path: /
_controller determine which controller is executed when the route is matched
host: "{user}.example.com"
_format set the request format (html, rss, json, ...) defaults:{_controller: AcmeDemoBundle:User:profile}
_locale set the locale on the request (the value will also be stored on the session) main_homepage: http://www.myurl.com/
path: /
methods set the request method (GET, POST, PUT, DELETE, HEAD)
defaults:{_controller: AcmeDemoBundle:Main:homepage}
http://andreiabohner.wordpress.com
http://andreiabohner.org
Routing
Symfony 2.2
USING ROUTES
Controller
Name of the route
Relative URL $url_index = $this->get('router')->generate('blog', array('culture' => 'en', 'page’ => 2));
$url_show = $this->get('router')->generate('blog_show', array('slug' => 'my-blog-post'));
// /blog/en/2
// /blog/my-blog-post
Absolute URL $url_index = $this->get('router')->generate('blog', array('culture' => 'en', 'page’ => 2), true);
// http://www.myurl.com/blog/en/2
To generate an
absolute URL,
$url_show = $this->get('router')->generate('blog_show', array('slug' => 'my-blog-post'), true);
pass true to the
// http://www.myurl.com/blog/my-blog-post
third argument
Template
Relative URL
Twig
PHP
{{ path('blog', {'culture': 'en', 'page': 2}) }} echo $view['router']->generate('blog', array('culture' => 'en', 'page' => 2))
{{ path('blog_show', {'slug': 'my-blog-post'}) }} echo $view['router']->generate('blog_show', array('slug' => 'my-blog-post'))
Absolute URL
Twig
PHP
{{url('blog', {'culture': 'en', 'page': 2}) }} echo $view['router']->generate('blog', array('culture' => 'en', 'page' => 2), true)
{{url('blog_show', {'slug': 'my-blog-post'}) }} echo $view['router']->generate('blog_show', array('slug' => 'my-blog-post'), true)
EXTRA
FOSJsRoutingBundle https://github.com/FriendsOfSymfony/FOSJsRoutingBundle
In the routes you want to use in your JS, you have to add the expose option:
Annotation
route_to_expose:
/**
pattern: /blog/{page}
YAML
Using:
Command Line
$ php app/console router:debug debug and visualize routes
$ php app/console router:match /blog/my-latest-post test which route match the given url
http://andreiabohner.wordpress.com
http://andreiabohner.org