Patrick
Patrick

Reputation: 129

Symfony2 - Requirement to provide a value value for an argument

The following exception is being returned when trying to use the method editAction() of a custom ProfileController herited from FOSuserbundle:

Controller "UserBundle\Controller\ProfileController::editAction()" requires that you provide a value for the "$id" argument (because there is no default value or because there is a non optional argument after this one).

UserBundle\Controller\ProfileController::editAction():

/**
 * Edit the user
 */
public function editAction($id)
{
    $user = $this
        ->getDoctrine()
        ->getManager()
        ->getRepository('UserBundle:User')
        ->find($id);

    if (!is_object($user) || !($user instanceof UserInterface)) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');

    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);

    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }

    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */

    $form = $this->createForm(new RegistrationEditFormType(), $user);
    $form->handleRequest($request);

    if ($form->isValid()) {
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');

        // Updating the user
        $userManager->updateUser($user);

        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);

        if (null === $response = $event->getResponse()) {
            $url = $this->generateUrl('lld_profile_show');
            $response = new RedirectResponse($url);
        }

        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

        return $response;
    }
    $request->getSession()->getFlashBag()->add('info-edit', 'Your account has been updated!');
    return $this->render('UserBundle:Profile:edit.html.twig', array(
        'form' => $form->createView(),
        'user' => $user
    ));
}

code snippet from admin.html.twig:

<tbody>
{% for user in users %}
<tr class="">
    <td>{{ user.id }}</td>
    <td>{{ user.firstName }}</td>
    <td>{{ user.lastName }}</td>
    <td>{{ user.amount }}</td>
    <td>
        {% if user.isAccountNonLocked %}
        <span class="label label-success">Enabled</span>
        {% else %}
        <span class="label label-default">Disabled</span>
        {% endif %}
    </td>

    <td>{{ user.date|date('m-d-Y H:i:s') }}</td>
    <td>{{ user.updatedAt|date('m-d-Y H:i:s') }}</td>
    <td></td>
    <td><a href="{{ path('lld_user_delete', {'id': user.id}) }}" class="label label-danger">Delete</a> <a
            href="{{ path('lld_user_deactivate', {'id': user.id}) }}" class="label label-default">Disable</a> <a
            href="{{ path('lld_user_activate', {'id': user.id}) }}" class="label label-success">Enable</a> <a
            href="{{ path('fos_user_profile_edit', {'id':user.id}) }}" class="label label-primary">Edit</a></td>
</tr>
{% endfor %}
</tbody>

The specific line being used:

 <td><a href="{{ path('lld_user_delete', {'id': user.id}) }}" class="label label-danger">Delete</a> <a href="{{ path('lld_user_deactivate', {'id': user.id}) }}" class="label label-default">Disable</a> <a href="{{ path('lld_user_activate', {'id': user.id}) }}" class="label label-success">Enable</a> <a href="{{ path('fos_user_profile_edit', {'id':user.id}) }}" class="label label-primary">Edit</a> </td>

adminAction():

public function adminAction()
{
    $user = $this->getUser();
    if (!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') || !$this->container->get('security.context')->isGranted('ROLE_ADMIN') || !$this->container->get('security.context')->isGranted('ROLE_MANUFACTURER')) {
        return $this->redirect($this->generateUrl('fos_user_security_login'));
    }

    $userManager = $this->container->get('fos_user.user_manager');
    $users = $userManager->findUsers();

    return $this->render('UserBundle:Admin:admin.html.twig', array(
        'users' => $users
    ));
}

public function deleteAction(Request $request, $id)
{
    if (!$this->container->get('security.context')->isGranted('ROLE_ADMIN')) {
        return $this->redirect($this->generateUrl('fos_user_security_login'));
    }

    $userManager = $this->container->get('fos_user.user_manager');
    $user = $userManager->findUserBy(array('id' => $id));

    if (null === $user) {
        throw new NotFoundHttpException("User with id " . $id . " doesn't exist.");
    }

    $userManager->deleteUser($user);
    $request->getSession()->getFlashBag()->add('info-delete', 'The user ' . strtoupper($user->getCompanyName()) . ' has been deleted successfully!');
    
    return $this->redirect($this->generateUrl('admin'));
}

So, I got stuck and I'm not seeing where this issue is coming from since the argument is logically being retrieved from the line where the click is being made. Obviously the right action (the custom editAction()) is being called.

Is there something I'm missing? Any suggestions please? I tried the check out similar posts but none could help me.

enter image description here

Upvotes: 0

Views: 661

Answers (1)

ozahorulia
ozahorulia

Reputation: 10094

The reason is that your are using native FOS routing, which does not have an id parameter at all.

<route id="fos_user_profile_show" path="/" methods="GET">
    <default key="_controller">fos_user.profile.controller:showAction</default>
</route>

It's a Profile Edit page, which allows you to edit your own profile, so the id must be taken from a session.

If you are trying to provide some kind of CRUD functionality, you have to either switch to a ready-to-use datagrid / admin systems (e.g. SonataUserBundle) or write your custom routing and controller.

Upvotes: 1

Related Questions