Laravel : To use $_GET or $_POST for search - php

In all laravel tutorials, they use the POST method for forms. For searching, I thought it was better to have the FORM use GET instead, so that as soon as the the form gets submitted, the query string is appended to the URL, and the controller can directly work on that.
If its a POST method, then the form is submitted to an intermediate controller, which processes it and redirects to some result controller. And if an intermediate controller is not used, the search strings will not be available in the URL anymore. I want to have my query string in the URL for various reasons.
Is it ok if I use the GET method in laravel forms?
{{ Form::open('search', 'GET') }}
Or are there any specific benefits of using the POST method?
Here is an example application built on laravel:
http://www.bootsnipp.com
The search page does not append any query strings. I don't the search to be like this.

Based on experience for searching always use GET. and for hidden data use post for example Login.
Even google us using get method for searching.

Laravel provides the options for controllers to be RESTful. By turning RESTful on, your controller can responds to RESTful commands such as POST, GET, DELETE and PUT
Using the Form::open() method, you can pick any of the four options. If you decide to use GET - then your get_method() will be called. If you decide to use POST - then your post_method will be called.
Following general RESTful commands, you would use POST to create a record. As you are retrieving a record, GET is the appropriate choice.

Related

PHP MVC Form Submission

I have a question related to form submission done in PHP application that's built in MVC architecture (self-written framework).
All examples that I've seen so far (including existing back-end frameworks) work this way that once form for adding record to database is submitted then certain method of controller is executed [say i.e. addRecord()], which triggers method of appropriate model. If everything goes OK then record is added and controller's method [addRecord() in this example] renders view of "index" page that displays table with records from database.
What I would like to achieve is to render view with form used to add records (the same that I used to add first record) instead of "index". Obviously I can do it easily by just rendering appropriate view from addRecord() (view with the form).
But the tricky point is when you check url you'll see the following:
The first time you enter it will be i.e.
http://project_name/my_controller/create
Once first form was submietted and you return to the view from addRecord() method then url will be:
http://project_name/my_controller/addRecord
What I would like to see is return to the original url, that is http://project_name/my_controller/create
Not sure if this is clear?
PS. Of course I could use AJAX call for form submission (that way I will stay at the same page) but perhaps it's possible to achieve the same without AJAX.
Thanks in advance,
On the controller you will want to submit to the addRecord route and do the processing. Have a check to make sure it was successful and on successful submission you can redirect back to the create route.
It is hard to give an example since you are using a custom made framework. I use slim which has a redirect method for a route. If what you have made does not have something like that then using should do the trick.
header('Location: '.$createUrl);
die(); //or exit

How can I send data from one page to another using Laravel?

So I'm using laravel 5.1 and I'm stuck in a weird situation.
I have 2 models Seasonand Tournament and lets say the url to add a Season model is ..../seasons/add and to view the season would be ..../seasons/(id)... standard stuff right?
But now I want to add a tournament from the link ..../seasons/1 and so I click a link that takes me to ..../tournaments/add... how can I send the Season model to that page without submitting a form with hidden input?
Currently I have this setup ..../seasons/1/tournaments/add using blade to generate the links. But this method just doesn't feel right....
Thanks.
How can I send data from one page to another using Laravel?: I would suggest that you do this from your controller. Take a look at Redirecting With Flashed Session Data, it might come in handy.
From the Flash Data documentation: "[...] it will only be available during the subsequent HTTP request, and then will be deleted [...]"
You can send your model, static values or whatever you want using ->with:
redirect('route')->with('key', 'value');

Laravel: Resource controllers and validation

I'm working with Resource controllers in Laravel. What I want to do is a really simple workflow.
1) using the create function to show the form to insert data
2) using the store function to save the resource
I can't understand which is the right way to build a simple environment like that, using validation.
Here some questions:
In the create function I show the form:
return View:make('form');
1) Where has to point the form's action? I suppose to the store function.
2) Where I have to perform validation? In my opinion if the form points to the store I can perform validation in this function. If validation fails I redirect to Create.
3) The "default" create function hasn't any parameter in input. How can I redirect to this function passing error messages?
What do you think about this workflow? How do you work with resource controllers?
The View::make method only makes your view. In your view, you need to make your forms. You can use the Form::open or Form::model methods inside your view along with all the other Form:: methods for inputs. Your controller should process the input and either your controller or model can save the input.
For #3, there isn't supposed to be any parameters for create as it's for a new record. show, edit, update, and destroy all accept an ID parameter.
See: http://laravel.com/docs/controllers#resource-controllers

CakePHP submitting forms: link vs post methods

I'd like to obtain some opinions about using link vs post form method types. For example, if I have a controller searches that finds products at the URL /searches/products, there are two ways to make this work.
With the link method, I can grab the data from $this->params['url'] as an array. With post, I can get them in $this->params['form'] using hidden form elements. This is assuming I am not using the CakePHP Form Helper. With the post method, the search string can be an argument for the action, so it seems like the way to go, but when would you ever go with the link method when submitting data?
If it's actually a search, I would almost always go w/ the URL method. It allows people to bookmark their results.

Symfony - Need a form filter to accept GET variable OR other method to create a filter link

Using Symfony 1.3
I have a normal form-filter that is used to filter values of a list,
it works normally. What I'd like to add is a link that is outside of the
form that can be used to filter by just a single criteria.
Anybody have a solution to this? Is there a way to set a filter to accept a GET?
You can approach this in 2 ways:
setting filters based on get parameters: symfony - admin module filters accessible as links
setting table method based on get parameters: Symfony doctrine admin generator list filters get method with no csrf token
I prefer second approach, because it gives you possibility to use filters on top your filtered list.
I resolved this in a much easier fashion than i suspected. The request already accepts GETS.
I just created a link with the parameters set as array values.
echo link_to('link_text', 'module/filter', array('query_string' => 'module_filters[field][text]='.$object->getField(ESC_RAW)))
Clicking on that link does exactly what I need.

Categories