Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller clean up #667

Closed
bodokaiser opened this issue Jun 20, 2012 · 2 comments
Closed

Controller clean up #667

bodokaiser opened this issue Jun 20, 2012 · 2 comments

Comments

@bodokaiser
Copy link

Hello,

I would like to make the controller a bit more readable and clean up some code rests I found.

Example of the ChangePasswordController, which is in my opinion much more readable than the master:

<?php

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace FOS\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;

/**
 * Controller managing the password change
 *
 * @author Thibault Duplessis <thibault.duplessis@gmail.com>
 * @author Christophe Coevoet <stof@notk.org>
 */
class ChangePasswordController extends Controller
{
    public function changePasswordAction()
    {
        $user = $this->getUser();
        if (!is_object($user) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        $process = $this->getUserFormHandler()->process($user);
        if ($process) {
            $this->setFlash('fos_user_success', 'change_password.flash.success');

            return $this->redirect($this->generateUrl('fos_user_profile_show'));
        }

        return $this->render('FOSUserBundle:ChangePassword:changePassword.html', array(
            'form' => $this->getUserForm()->createView()
        ));
    }

    protected function getUserForm()
    {
        return $this->container->get('fos_user.change_password.form');
    }

    protected function getUserFormHandler()
    {
        return $this->container->get('fos_user.change_password.form.handler');
    }

    protected function setFlash($action, $value)
    {
        $this->container->get('session')->setFlash($action, $value);
    }
}

As you see I have switched from the ContainerAware to the above layer, the controller.
Is there a specific reason why not to use the Controller but the ContainerAware?

@stof
Copy link
Member

stof commented Jun 20, 2012

the reason is the best practice advocated in the doc: http://symfony.com/doc/current/cookbook/bundles/best_practices.html#controllers

@bodokaiser
Copy link
Author

So I assume that the other shortcuts also don't belong to the best practise (even they also would increase reability)?

@stof stof closed this as completed Sep 8, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants