This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Can't figure out why it won't generate my url...

New topics about Symfony 2 should go here

Can't figure out why it won't generate my url...

by bpolaszek » Tue Apr 28, 2015 1:34 pm

Hello,

I'm using Symfony 2.6 and I encounter an unexpected problem on the generateUrl() method in a controller.

Have a look at this :
Code: Select all
    /**
     * Add / edit a node
     * @Route("/c-{client}/api-{api}/nodes/add", name="node_add")
     * @Route("/c-{client}/api-{api}/n-{node}/edit", name="node_edit")
     * @Method({"GET", "POST"})
     * @Security("has_role('ROLE_CHUCKNORRIS')")
     * @ParamConverter("client", options={"mapping": {"client": "identifier"}})
     * @ParamConverter("api", options={"mapping": {"api": "identifier"}})
     * @ParamConverter("node", options={"mapping": {"api": "identifier", "node": "identifier"}})
     * @Template("Client/FrontManager/NavigationNodes/add.html.smarty")
     * @param Request        $request
     * @param Client         $client
     * @param Api            $api
     * @param NavigationNode $node
     * @return array|RedirectResponse
     */
    public function editAction(Request $request, Client $client, Api $api, NavigationNode $node = null) {
        $node   =   $node ?: new NavigationNode($api);
        $isNew  =   $node->isNew();
        $form   =   $this->createForm($this->get('sbs.form.node.manager'), $node);

        $form->handleRequest($request);

        if ($form->isValid()) {

            $this->getDoctrine()->getManager('client')->persist($node);
            $this->getDoctrine()->getManager('client')->flush($node);

            $this->flash('success', "Node successfully edited");
            return new RedirectResponse($this->generateUrl('node_edit', [
                'client' => $client,
                'api'    => $api,
                'node'   => $node,
            ]));
        }

        $this->smarty()->assign('form', $form->createView());
        return [
            'node'  => $node,
        ];
    }


As you can see, this method both handles an add form and an edit form, for a NavigationNode entity, relying on an Api entity and a Client entity.

There are 2 routes : node_add and node_edit.

When I call /app_dev.php/c-charly/api-default/nodes/add a new NavigationNode is created based upon the "default" api and the client named "charly".
When I call /app_dev.php/c-charly/api-default/n-root/edit the "root" node is successfully fetched from the database.

Both urls work when I call them in my browser.
Whenever the node_add or the node_edit route is called, I've got always a $client object, an $api object and a $node object, which are later forwarded to the generateUrl() method.

The problem is on $this->generateUrl() which results in a ContextErrorException : Warning: preg_match() expects parameter 2 to be string, object given triggered in Symfony\Component\Routing\Generator\UrlGenerator::doGenerate()

I tried to generateUrl() the node_edit route from another controller, fetching manually the required entities (client, api, node) and I'm still getting this error.

Any ideas ??

Thanks,
Ben
bpolaszek
Junior Member
 
Posts: 2
Joined: Sat Nov 29, 2014 11:28 pm

Re: Can't figure out why it won't generate my url...

by karop » Sun May 17, 2015 1:41 pm

Try to debug this checking what exactly type of the second parameter is, apparently it doesn't recognize it as a string.

expects parameter 2 to be string, object given


http://php.net/manual/en/function.gettype.php
Or even:
Code: Select all
print_r(parameter2);
karop
Junior Member
 
Posts: 13
Joined: Tue Oct 02, 2012 4:25 pm