set variables to twig when redirect link? - php

i want to set a variable to twig when redirect link like when use the render methods :
return $this->render('socialBundle::index.html.twig',array(
'id' => $id
));
it will set the Id variable to twig : {{ id }}
return $this->redirect($this->generateUrl("tuto_animaux_voir", array(
'id' => $id
)));
it will set the Id variable to the link : xxxxx.com/Id , i want that it render this variable to twig when redirect ...
sorry for this bad concept and language because i'm not england , and this is my first question in stackoverflow ,
wait for a reponse , thanks

I don't quite understand what you want to do, if you want to put a link in a twig template or just redirect from a controller according a condition. if you can give more details of what you want to do I think I can help you.
UPDATE (solution)
You could use "Forwarding to Another Controller" instead of using "Redirect" and from there to send the variables to the twig template

Based on your last comment, I came to the following conclusion.
Assume that you have index action, with route tuto_animaux_voir. And route has one parameter id. It takes id as parameter and fetches alert from the database(we have alerts on the database). Also we have entity Alert with fields id(default), title.
...
public function indexAction($id){
$alert = $this->getDoctrine()->getEntityManager("AppBundle:Alert")->find($id);
return $this->render("AppBundle:Post:index.html.twig", array(
'alert' => $alert
));
}
In your template index.html.twig you can show your alert.
...
{{ alert.title }}
...
If you want to redirect to the index action in the twig.
<a href="{{ path("tuto_animaux_voir", { 'id': alert_id })}}">Alert Title<a>
Also if you want render this action as partial.
{{ render(controller('AppBundle:Alert:index',{'id': alert_id})) }}
I think my answer would be helpful for you.

In you case 'tuto_animaux_voir' is a route associated to controller action. Given id will be available as action parameter, so you can pass it like this:
public function fooAction($id)
{
return $this->render(
'socialBundle::foo.html.twig',
['id' => $id]
)
}
You can also fetch data from database and pass them to the view.
If that's not what to you want to achieve, tell us why you want to use redirect, and what these two actions should do.
Edit:
If I understood your comment correctly, you want to implement flash messages, which are already available in symfony.
Example in docs is simple and self-explanatory.

Related

How to pass the number of current page in render()?

I print the pagination box via this:
{{ $myarray->render() }}
But here is a problem, when I click on each page-link, the URL contains only one argument which is ?page={a number} .. while I need to keep some other arguments. So I need to pass them in render() function to have them on other pages. I mean I want an URL like this
http://example.com/products?page=2&order=desc&q=something
See? In addition to page=2 there is also two other arguments (parameters) in the URL. How can I do that in Laravel?
You want to use appends() method:
{{ $myarray->appends(['order' => 'desc', 'q' => 'something'])->render() }}

Passing an id from a select list in a form, getting "non-object" error, Laravel-4

On a dashboard page, I've created a select list in a form that lists the names of components; the value that's passed from the select list is obviously the component id. On pressing submit, the user is routed to a page that displays the data about that component. Should be dirt simple...
Controller:
public function showDashboard()
{
$components = Component::lists('name','id'); ...
return View::make('dashboard', array('components'=>$components, ...))
}
dashboard.blade.php:
{{ Form::open(array('route' => array('components.show', $components->id), 'method'=>'get')) }}
{{ Form::Label('id','Component:') }}
{{ Form::select('id', $components) }}
{{ Form::submit('Show Component', array('class'=>'button')) }}
{{ Form::close() }}
I've tried various ways of doing this, and get a different error every time. The above code doesn't even let me display the dashboard page -- I get a "Trying to get property of non-object" error. Clearly, it's not liking $components because that was passed as a list array and not an object. As I said, I'm sure this is dirt simple, I just can't figure out the proper syntax, and Laravel docs aren't giving me the answer. Thanks!
The problem isn't the dropdown, or the lists method, but rather in your form opening. Here, you have $components->id as an argument to the route, but $components is an array and you can't access an id property on it.
Finally figured this out. I had posted a similar question here subsequent to this one, and rather than repeat the answer, it is here:
How to pass id value from select list to controller when controller expects an object? laravel-4
The very short version: change Route::get to Route::post. Details with code in the link above. Problem solved!

Laravel3 how to send variable into controller from view

I'm learning laravel 3 at the moment, and I have a technical question. I have a list of authors and I'm trying to make some links to set a filter to order those authors.
I tried setting a default parameters to order by Name, it works, but I can't pass any other filters.
This is my route :
Route::get('authors', array('as'=>'authors', 'uses'=>'authors#index'));
And this is my controller function :
public function get_index($filter="name"){
return View::make('authors.index')
->with('title', 'Authors list')
->with('authors', Author::order_by($filter)->get());
}
And this is the links in my view trying to send the filter I want
{{ HTML::link_to_route('authors', 'Id', array('id')) }}
{{ HTML::link_to_route('authors', 'Name', array('name')) }}
The parameters I try to send (id and name) from the view never reach the controller so it always use default parameter.
Thank you !
You're not setting any route parameters, you must do something like:
Route::get('authors/(:any?)', array('as'=>'authors', 'uses'=>'authors#index'));
Take a look at the docs: http://three.laravel.com/docs/routing#wildcards

Laravel updating database route

I'm trying to update a column in my database with a total of votes on a project. I'm having a 404 problem but I don't see the problem in my route. (PS: Laravel 3)
This is my Vote button on every project :
{{ Form::open('project/addvote', 'VOTE') }}
{{ Form::hidden('id', $project->id) }}
{{ Form::submit('Vote') }}
{{ Form::close() }}
So when you click on the vote button it use this route :
Route::put('project/addvote', array('uses'=>'projects#addvote'));
And this is my action in the projects controller (no updating yet, just trying to redirect) :
public function put_addvote(){
return Redirect::to_route('project', $id)
->with('message', 'Vote successful');
}
Redirecting to this route :
Route::get('project/(:num)', array('as'=>'project', 'uses'=>'projects#project'));
And this is getting me the 404 error
Thanks to every response and the great help here!
Actually, Redirect::to_route expects a name of a route to redirect to it and a named route has to be declare with a name, like,
Route::put('project/addvote', array('as' => 'project', 'uses'=>'projects#addvote'));
So, you can use it;s name to redirect to it like
return Redirect::to_route('project');
Here, project has been used as it's name using 'as' => 'project'. In, your example, you didn't gave any name to the route, here
Route::put('project/addvote', array('uses'=>'projects#addvote'));
The, as => 'route_name' is missing.
For second question, you can do it as
$id = Input::get('id');
Project::find($id);
Project->votenumber = 5;
Project->->save();
Inserting & Updating Models.
Update :
It was a bit confusing but after a conversation through the commenting system the answer for routing is give below :
You (OP) mentioned that you have a route declared as
Route::get('projets', array('as'=>'projets', 'uses'=>'projets#index'));
To this route you are trying to redirect using this
return Redirect::to_route('project', $id);
So, you are passing a parameter and it's not in your route declaration and this is the problem, so to overcome this change your route declaration to this
Route::get('projets/(:num)', array('as'=>'projets', 'uses'=>'projets#index'));
Or
Route::get('projets/(:any)', array('as'=>'projets', 'uses'=>'projets#index'));
Or, you can make the param optional using a ?, for example :
Route::get('projets/(:any?)', array('as'=>'projets', 'uses'=>'projets#index'));
Check wildcard Routes.
Update :
You should have postd the original code with the question, anyways, Change this
return Redirect::to_route('project', $id);
to
return Redirect::to_route('project', array($id));

Simple redirect in Symfony2 by changing a query string parameter, NOT route related

This should be simple, and I've been searching all over Google, but I keep coming up with 'route' related advice.
I just want to perform a redirect to the same page and modify one of the query string parameters (either to clear one or set one).
I can't see how to do this anywhere.
An option could be to completely generate the URL manually and use this I guess, but that doesn't seem a very good method:
$this->router->generate("http://domain.com?a=1")
I hope I understand what you intend to do... In your controller (?) use
$this->generateUrl(
$request->attributes->get('_route'),
array_merge(
$request->query->all(),
array('param' => 'val') // change the param
)
);
to generate the url.
What is the reason of this redirect? I suppose that you wanna redirect from controller, don't you? I'm not sure what result you wanna achive. You have to be careful with redirecting in controller for same action controller (redirect loop).
However, in controller you can do that by:
public function indexAction()
{
// ...
return $this->redirect($this->generateUrl($request->attributes->get('_route'), array('paramName' => $paramValue)));
}
In my opinion, you should consider writing an event listener: http://symfony.com/doc/current/book/internals.html#handling-requests

Categories