skip to Main Content

im learning Symfony 5 and was wondering if it is okay to put long strings / "messages" inside a controller (ex. addFlash message here), or there is a better way of storing them.

I think that my solution could become problematic when i try to translate my website to a different language.

class RegisterController extends AbstractController
{

    public function index()
    {

        if ($form->isSubmitted() && $form->isValid()) {

            ...

            $this->addFlash(
                'info',
                "Congratulations {$user}! You are now a part of growing <b>Symf</b> community! <br>
                An activation link to <b>{$user->getEmail()}</b> was <u>not</u> sent because mailing is not implemented. 
                &nbsp <small>(...yet)</small>"
            );

        }

        ...
        
    }
}

Tutorials and Symfony docs are doing it in controller but i know that might be just an example.

So my question is, is it a good practice to put long strings in variables/values in controller or is there a better way?

2

Answers


  1. It’s probably a better practice to put those in a translation file. More information https://symfony.com/doc/current/translation.html

    Login or Signup to reply.
  2. Technical alternatives

    To provide alternatives to Marks suggestion of translation (which might not be useful now, but could become in the future), you could also use a method or const:

    class RegisterController extends AbstractController
    {
        private const WELCOME_MESSAGE = "Congratulations %s You are now a part of growing <b>Symf</b> community! <br>
                    An activation link to <b>%s</b> was <u>not</u> sent because mailing is not implemented. 
                    &nbsp <small>(...yet)</small>";
    
        public function index()
        {
            $this->addFlash('info', sprintf(self::WELCOME_MESSAGE, $user, $user->getEmail()));
        }
    }
    

    While it does remove noise from the actual logic, this is not great, a const this big feels clonky. What if you want to add more text? Maybe a link to a FAQ? Lets see how a private function does:

    class RegisterController extends AbstractController
    {
        public function index()
        {
            $this->addFlash('info', $this->getWelcomeMessage($user));
        }
    
        private function getWelcomeMessage(User $user): string
        {
            return "Congratulations {$user}! You are now a part of growing <b>Symf</b> community! <br>
                    An activation link to <b>{$user->getEmail()}</b> was <u>not</u> sent because mailing is not implemented. 
                    &nbsp <small>(...yet)</small>";
        }
    
    }
    

    This is also not great, because the task of the controller is to control, not to manage texts. There are now two reasons to change this controller, which violates single responsibility.


    But the (IMO) real solution:

    Dont use toasts for this. Toasts are for short messages like "✓ Success" or "Confirmation mail sent". You’re now "abusing" this for something it’s not intended for and you can feel "resistance" in the implementation.

    Just redirect the $user to a welcome page and use that to explain what is going on. You can add a little about a spambox check, or maybe some image to set the vibe.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search