A
A
Alexander2016-12-05 21:51:03
symfony
Alexander, 2016-12-05 21:51:03

Is it possible to simplify routing in symfony?

It is necessary to make the same type of routing in symfony 2.8 for different pages.
Router debug itself:
b60c331be5ab4ac9bfda0484dee87ad4.png
The idea is that there will be more routes like /post, /comment. Perhaps there will also be another route like /best, /worst
Is it possible to simplify routing using inheritance? or what other options? or to write completely dynamic routing?
At the moment it looks terrible:

BlogBundle_best:
  path: /best
  defaults: { _controller: "BlogBundle:Best:index" }
  methods: [GET]

BlogBundle_best_post:
  path: /best/post
  defaults: { _controller: "BlogBundle:Best:post", page: 1 }
  methods: [GET]

BlogBundle_best_post_page:
  path: /best/post/page/{page}
  defaults: { _controller: "BlogBundle:Best:post" }
  methods: [GET]
  
BlogBundle_best_post_period:
  path: /best/post/{period}
  defaults: { _controller: "BlogBundle:Best:postByPeriod", page: 1 }
  methods: [GET]
  requirements:
    period: "ever|year|month|week|day"
    page: \d+

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2016-12-05
@TehekOne

// routing.yml
BlogBundle
    resource: "BlogBundle.yml"
    prefix: /

// BlogBundle.yml
BlogBundle_best:
    resource: "BlogBundle_best.yml"
    prefix: /best

// BlogBundle_best_post.yml
BlogBundle_best_post:
    resource: "BlogBundle_best_post.yml"
    prefix: /post
BlogBundle_best_comment:
    resource: "BlogBundle_best_comment.yml"
    prefix: /comment


// BlogBundle_best_post.yml
BlogBundle_best_post_period:
    path: /{period}  <-- real /best/post/{persiod}
    ...
BlogBundle_best_post_page:
    path: /page/{page}  <-- real /best/post/page/{page}
    ...
...

A
Anton, 2016-12-05
@sHinE

In symfony, it seems that it is possible to set routes through php - use this method and generate routes in a loop with the necessary prefixes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question