Redirecting in service - symfony2

前端 未结 5 507
南方客
南方客 2021-01-17 22:03

May I perform redirection to another controller within service?

I have implemented a service based on example provided by @Artamiel.

My function code which i

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 22:54

    I am agree with @Moritz that redirections must be done inside controllers.

    But sometimes happens that some redirection would be useful if done from a local function inside a controller.

    In this case a solution could be to create a custom Exception Class (RedirectException).

    Here you are a link to an interesting post: https://www.trisoft.ro/blog/56-symfony-redirecting-from-outside-the-controller.

    I know it's debatable, but has sense if it's used within a local function inside its Controller.

    To summarize, the steps would be:

    1) Create a RedirectException class

    2) Create a Kernel Listener

    3) Set Up the Service in the configuration service file (service.yml)

    4) Throw an exception with the url to redirect when necessary.

提交回复
热议问题