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() }}
Related
I have never understood how to create proper URLs. Every time I end up with trying to figure out if I should do ?var=value or &var=value and then if ?var=value already exists then I end up with ?var=value&var=value.
P.S. I am working with Laravel. (So maybe there is a built-in function?)
For example:
I have pagination and my URL could look like this
www.example.com OR
www.example.com?name=John
Then my pagination link is href="?page=2" and I end up with
www.example.com?name=John?page=2
Then I want to navigate to the next page with href="?page=3" and I end up with this. Because it keeps on adding.
www.example.com?name=John?page=2?page=3
What a mess.... is there a function for PHP or Laravel that would create proper URLS? (knowing when to use ? or & and not add existing values all the time but replace them if they exist already.
If you're using Laravel I think you should use some helper functions:
route('user.profile', ['id' => 1]);
It will create url by route name and parameters, It will look like:
http://sitename/user/profile?id=1
And you can find some useful helper functions here
There should be one and only ? in URL's which separates URI and parameters, rest of the key-value pairs are separated by &, and if you need to pass & or ? as a parameter, then you need to encode them.
you could pass an array to http_build_query and it will build the url for you
and in laravel there are url helper functions you can use them with ease.
Laravel come with a route helper which allow you to generage url quickly by passing the name with which you register your route.
Route::get('/', 'HomeController#index')->name('home.route')
Here you can see I pass home.route to the name method which is the name which I use for this route. And when I want to generate the URL for that route in my view I will juste do
{{ route('home.route') }}
If ny route take some parameters like this
Route::get('/person/{id}', 'HomeController#index')->name('home.route')
In view I will generate the url like this
{{ route('home.route', ['id' =>$id]) }}
Because you want juste to make pagination, Laravel comme with a buil in pagination. When you want to paginate something in your views, you must just call the paginate function on your model and laravel will handle all the route for that. For example I you have a Person Model you can do that like this in your controller
$persons = Person::paginate(25)
And in your views to generate pagination for that you will have to do
{{ $persons->links() }}
And that's all
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.
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!
{{ render(controller("SomeBundle:Foo:Bar", {HERE I WANT TO PASS ALL query parameters app.request.query.all}) }}
So can I access all master request query parameters in sub request and the subrequest should also run independently?
Try this:
{{ render(controller("SomeBundle:Foo:bar", {'all': app.request.query.all}) }}
and in action store it in $all variable
public function barAction($all) {
// other your code
}
From your controller:
array_merge($request->query->all(), $request->get('_route_params'));
//query->all : get all query string parameters
//_route_params : get current route parameters
From your twig template must be sth like:
app.request.query.all|merge(app.request.attributes.get('_route_params'))
I've never used this in twig templates, so first test it ;)
Then you can use that functions however you want to build the variables you'll pass to your subrequest
To just pass in what is in app.request.query.all:
{{ render(controller("SomeBundle:Foo:Bar", app.request.query.all)
To merge something extra in:
{{ render(controller("SomeBundle:Foo:Bar", { something: 'extra' }|merge(app.request.query.all))
Tested in Symfony 3.3.10 and Twig 1.35.0
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