I want to redirect and send my request to another route but this method is not working.
return $this->redirect($this->generateUrl(
'admin_platform_create',
array('request' => $request)
));
When I arrive into admin_platform_create it doesn't pass that if
public function createAction(Request $request)
{
if ($request->getMethod() == 'POST')
{
Though this is working,
return $this->forward('AdminPlatformBundle:Manage:edit',
array('request' => $request));
It is not what I need as it is not writing the new URL.
Thanks for your help!
If you are using Symfony 2.6 or later, you can do it:
return $this->redirectToRoute('route', [
'request' => $request
], 307);
Related
I use the forward-method to forward requests to another Controller action:
$response = $this->forward($menuItemConfig['controller'] . ':' . $actionParams['action'], [
'request' => $request,
]);
This is the action of the Controller - post params are empty.
public function delete(Request $request): Response
{
if ($request->isMethod('POST') == false) {
throw new AccessDeniedException('Only POST allowed for this action.');
}
$postParams = $request->request->all();
dump($postParams);
die;
}
Passing parameters to the action does not work. All not-DI-params of the action are generally empty.
Symfony version 5.2.3
I have noticed that you are missing :, try this:
$response = $this->forward($menuItemConfig['controller'] . '::' . $actionParams['action'], [
'request' => $request,]);
This is my simple code
class LuckyController extends Controller
{
public function taskFormAction(Request $request)
{
$task = new Task();
//$task->setTask('Test task');
//$task->setDueDate(new \DateTime('tomorrow noon'));
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, array('label' => 'Save'))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$task = $form->getData();
return $this->redirectToRoute('task_ok', array('task' => '123'));
}
return $this->render('pre.html.twig', array('pre' => print_r($task, true), 'form' => $form->createView()));
}
public function taskOKAction(Task $task)
{
return $this->render('ok.html.twig', array('msg' => 'ok', 'task' => print_r($task, true)));
}
}
and this line
return $this->redirectToRoute('task_ok', array('task' => '123'));
makes redirection to taskOKAction, but it lets me just send parameters by URL (?task=123).
I need to send object $task to taskOKAction to print on screen what user typed in form.
How can I do that? I've already red on stackoverflow before asking that the good solution is to store data from form (e.g. in database or file) and just pass in parameter in URL the ID of object. I think it's quite good solution but it adds me responsibility to check if user didn't change ID in URL to show other object.
What is the best way to do that?
Best regards,
L.
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)
From your form action you can do it without actual 302 redirect:
public function taskFormAction(Request $request)
{
$task = new Task();
//$task->setTask('Test task');
//$task->setDueDate(new \DateTime('tomorrow noon'));
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, array('label' => 'Save'))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$task = $form->getData();
return $this-taskOKAction($task)
}
}
This is perfectly legal, and your frontend colleague will thank you for lack of the redirects.
I want to create a pdf file out of some route-dependant data
{http://example.com/products/123/?action=update}
$app->finish(function (Request $request, Response $response) {
// Make a pdf file, only if:
// - the route is under /products/
// - the action is update
// - the subsequent ProductType form isSubmitted() and isValid()
// - the 'submit' button on the ProductType form isClicked()
});
As a normal form submission process I have:
public function update(Application $app, Request $request)
{
$form = $app['form.factory']->create(ProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (!$form->get('submit')->isClicked()) {
return $app->redirect('somewhere');
}
$product = $form->getData();
$app['em']->persist($product);
$app['em']->flush();
return $app->redirect('product_page');
}
return $app['twig']->render('products/update.html.twig', array(
'form' => $form->createView(),
));
}
Questions:
Should I duplicate all of the conditionals in finish middleware?
How to access the Product entity in finish middleware?
Consider having multiple resource types like Products, Services, Users, ...
I am trying to register user from index page but When validator failed then want to redirect to register page.
I am tired to solve this problem . can't customize Illuminate/Foundation/Validation/ValidatesRequests.php page.
Here is the code
protected function getRedirectUrl() {
return route('register');
}
protected function validator(array $data) {
$this->getRedirectUrl();
return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]);
}
add the below method which generate the previous url in your controller and override the default one add following methods in your controller
in your controller where you have defined $this->validate call define below method and use Request
use Illuminate\Http\Request; // add at the top
protected function getRedirectUrl() {
return route('register');
}
protected function validator(array $data) {
return $this->validate(request(), [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]);
}
public function register(Request $request)
{
$this->validator($request->all());
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
I am having a code for login which is in my AuthController like this.
public function login(Request $request){
$email = $request->input('email');
$password = $request->input('password');
$validation = array(
'email' =>'required',
'password' => 'required');
$validator = Validator::make($request->all(), $validation);
if ($validator->fails()) {
$messages = $validator->messages();
return redirect('login_with_assismo')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
if (auth()->authenticate()) {
return redirect()->intended('welcome');
}
}
}
When i use this is i think login performs but it redirect me to the page somthng like this
localhost:8000/login
Anyone help me how to authenticate login am i doing something wrong or what. Please get the solution for this.
You can use attempt() function to login the user as:
public function login(Request $request)
{
$inputs = $request->only('email', 'password');
$rules = array(
'email' =>'required',
'password' => 'required'
);
$validator = Validator::make($inputs, $rules);
if ($validator->fails()) {
$messages = $validator->messages();
return redirect('login_with_assismo')
->withErrors($validator)
->withInput($request->except('password'));
}
if (auth()->attempt($inputs)) {
return redirect()->intended('welcome');
}
return redirect()->back()->withInput();
}
You may want to use the Auth facade, as described here:
https://laravel.com/docs/5.3/authentication#authenticating-users
Dont panic bro its because of Auth middleware go with #amit's code just go to
App\Http\Middleware\RedirectIfAuthenticated
and replace this route with your's
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
after that you are good to go