Search code examples
symfonyannotationssymfony3

Symfony 3, is it possible to use a variable in a path?


I am working on a project, and I would like to be able to use variables in paths directly. This may seem strange, but imagine I have

mysite.org/admin

I take precautions to stop people accessing admin, ofcourse. But for peace of mind, and the clients peace of mind, it would be nice to allow the client to choose this path, and even change it every so often.

Yes I could just change the path manually, and it would take 2 seconds, but it would just be nice if this was a possibility at all.

I'm using annotation for my routing, but I am interested in any answers if not just for curiosities sake.

Is it possible to store this in config.yml, and then use this in your path? I have been searching but not found anything on the subject.


Solution

  • You could do something like this:

    
         /**
         * @Route("/admin/{admin_path}")
         */
        public function adminAction(Request $request, $admin_path)
        {
            //code to verify if admin_path is currently the good one using database or another way
            if (false === is_admin_path_the_right_one($admin_path)) {
                $this->redirectToRoute('index');
            }
    
            //show admin
        }
    
    

    I prefixed with /admin because otherwise every other route would match this action. But you could use something else like /a/