Symfony Redirect with 2 Parameters

symfony redirect with 2 parameters

Well, that's normal, "redirect" redirect to an absolute URL. You can do that:

$this->redirect($this->generateUrl('default', array('module' => 'input',
'action' => 'new', 'year' => $year, 'month' => $month)));

Symfony 3 Redirect to another routhe with url parameter

Actualy you're almost done ;) TL;DR just use

return $this->redirectToRoute('person_in_need_view', ['id' => $yourEntity->getId()]);

I'm a bit curious why you get data via $form->get('<FIELD_NAME>)->normData() but there're posibly a reson for that...

I hope you've configured your PersonInNeedType for your PersonInNeed Entity. If so, just call

if ($form->isSubmitted() && $form->isValid()) {
/** @var PersonInNeed personInNeed **/
$personInNeed = $form->getData();

//at the end...after persist and flush so your entity is stored in DB and you have an ID
return $this->redirectToRoute('person_in_need_view', ['id' => $personInNeed->getId()]);
}

Symfony: pass parameter between actions (with a redirect)

Why dont you use sessions to store values before redirecting, and then getting them on the other action after you redirected? like:

class ActionClass1 extendes sfActions
{
public function executeAction1(sfWebRequest $request)
{
[..]//Do Some stuff
$this->getUser()->setAttribute('var',$variable1);
$this->redirect('another_module/action2');
}
}

class ActionClass2 extends sfActions
{
public function executeAction2(sfWebRequest $request)
{
$this->other_action_var = $this->getUser()->getAttribute('var');
//Now we need to remove it so this dont create any inconsistence
//regarding user navigation
$this->getUser()->getAttributeHolder()->remove('var');
[...]//Do some stuff
}
}

Symfony 4 - How to set entity id for route in redirect after form submit

Once the new entity is persited & flushed, you can use it like:

 $em->persist($booking);
$em->flush();

return $this->redirectToRoute('profile_booking_show', ['id' => $bookig->getId()]);

Can the method redirectToRoute() have arguments like render()?

When you call redirectToRoute($route, array $parameters) from a controller, $parameters is used to generate the url tokens, not variables to render in view, this is done by the controller assigned to the route you are redirecting to.

example :

class FirstController extends Controller
{
/**
* @Route('/some_path')
*/
public function someAction()
{
// ... some logic
$entity = 'some_value';

return $this->redirectToRoute('some_other_route', array('entity' => $entity)); // cast $entity to string
}
}

class SecondController extends Controller
{
/**
* @Route('/some_other_path/{entity}', name="some_other_route")
*/
public function otherAction($entity)
{
// some other logic
// in this case $entity equals 'some_value'

$real_entity = $this->get('some_service')->get($entity);

return $this->render('view', array('twig_entity' => $real_entity));
}
}

Symfony - parameters syntax and redirect

I defined my custom route in parameters.yml in my Symfony project.

// routes.yaml    
custom_routes:
resource: 'custom_routes.yaml'
prefix: /

// custom_routes.yaml
front_url:
path: '%front_url%'
defaults:
_controller: App\Controller\HashController:hash
hash: false

parameters:
front_url: 'https://my.custom-route.net/email/confirm/{hash}'

How to pass correctly object using redirectToRoute?

Use the parameter converter and annotate the route properly.

Something like (if you use annotation for routes)

/**
* @Route("/task/ok/{id}")
* @ParamConverter("task", class="VendorBundle:Task")
*/
public function taskOKAction(Task $task)

You can also omit the @ParamConverter part if parameter is only one and if is type hinted (as in your case)



Related Topics



Leave a reply



Submit