1
votes

I am new to symfony. As an exersice I`m trying to make some basic cms.And I was wondering is this aproach of routing wrong:

  /**
     * @Route("/back-office/", name="back-office")
     */
    public function indexAction(Request $request,$page="")
    {
      switch($page){
        case "":
            return $this->render('CmsBundle:BackOffice:index.html.twig');
        break;
        default:
            return $this->render('CmsBundle:BackOffice:site-map.html.twig');
        break;
      }
    }

This is my yaml confing:

back_office_pages:
    path: /{page}
    defaults: { _controller: CmsBundle:BackOffice:index}

By using this aproach I wont have to configure each route in the yaml file. Since routes may vary. But I am not quite sure this is the symfony way of doing things so I decided to ask for advice..

What I`m trying to achive: Lets say we have a user that have less back-end programing expirience or not at all and he stumbuled upon the CMS. The goal is to add front end pages using some user interface. Then we store the pages(slug) in the database. In the index action we retrive this data. From the database we can also assing template to a page (we need the user to have at least some html+css+twig).

So what we do is get the pages that user added : ex : Gallery, Contacts we check the request url and if the page requested is in the array from the database we return the template related to the page.

NOTE: If you disagree with this method please do not bash me but eplain why is this wrong. Because as I said I am still new with the framework.

1
You are using annotations instead of YAML to configure routes, aren’t you?Lumen
Well as I understood the documentation I need to have them both. The annotation and the yaml config ex: back_office_pages: path: /{page} defaults: { _controller: CmsBundle:BackOffice:index} I`ll add it to the questionNewbie
You should rename your path to page or vice versa. Also it's better to have one action, one template per page.Turdaliev Nursultan
I edited path to page :) my mistake. But Im trying to figure out why should I have one action per template I mean I do for the clarity of the code of course. But wouldnt this make it harder to maintain from other point of view each time you are changing a page you have to make the confingurations to the yaml as well. Clearing cache etc. And the goal of this CMS is to have somewhat dynamic template manager. I can write some script that edits the yaml but that sound like security issue to me :).Newbie
It's really unclear... First I confirm that you must use either YAML config or Annotation config (see the routing.yml file to see which one is used). Second, I don't understand your Action ...Byscripts

1 Answers

1
votes

Try setting your routing to:

back_office_pages:
    resource: "@CmsBundle/Controller/"
    type: annotation

to set up Routing Annotations inside your CmsBundle.

Then, your action should be working using the url "/back-office/{page}"