How To Pass GET Parameters To Laravel From With GET Method ?

后端 未结 7 869
余生分开走
余生分开走 2020-11-30 02:05

i\'m stuck at this very basic form, that i could not accomplish, which i want to build a search form with an text input, and two select controls, with a route that accept 3

相关标签:
7条回答
  • 2020-11-30 02:16

    I had same problem. I need show url for a search engine

    I use two routes like this

    Route::get('buscar/{nom}', 'FrontController@buscarPrd');
    
    Route::post('buscar', function(){
    
       $bsqd = Input::get('nom');    
    
       return Redirect::action('FrontController@buscarPrd', array('nom'=>$bsqd));
    
    });
    

    First one used to show url like we want

    Second one used by form and redirect to first one

    0 讨论(0)
  • 2020-11-30 02:28

    The simplest way is just to accept the incoming request, and pull out the variables you want in the Controller:

    Route::get('search', ['as' => 'search', 'uses' => 'SearchController@search']);
    

    and then in SearchController@search:

    class SearchController extends BaseController {
    
        public function search()
        {
            $category = Input::get('category', 'default category');
            $term = Input::get('term', false);
    
            // do things with them...
        }
    }
    

    Usefully, you can set defaults in Input::get() in case nothing is passed to your Controller's action.

    As joe_archer says, it's not necessary to put these terms into the URL, and it might be better as a POST (in which case you should update your call to Form::open() and also your search route in routes.php - Input::get() remains the same)

    0 讨论(0)
  • 2020-11-30 02:30

    So you're trying to get the search term and category into the URL?

    I would advise against this as you'll have to deal with multi-word search terms etc, and could end up with all manner of unpleasantness with disallowed characters.

    I would suggest POSTing the data, sanitising it and then returning a results page.

    Laravel routing is not designed to accept GET requests from forms, it is designed to use URL segments as get parameters, and built around that idea.

    0 讨论(0)
  • 2020-11-30 02:32

    An alternative to msturdy's solution is using the request helper method available to you.

    This works in exactly the same way, without the need to import the Input namespace use Illuminate\Support\Facades\Input at the top of your controller.

    For example:

    class SearchController extends BaseController {
    
        public function search()
        {
            $category = request('category', 'default');
            $term = request('term'); // no default defined
    
            ...
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:38

    I was struggling with this too and finally got it to work.

    routes.php

    Route::get('people', 'PeopleController@index');
    Route::get('people/{lastName}', 'PeopleController@show');
    Route::get('people/{lastName}/{firstName}', 'PeopleController@show');
    Route::post('people', 'PeopleController@processForm');
    

    PeopleController.php

    namespace App\Http\Controllers ;
    use DB ;
    use Illuminate\Http\Request ;
    use App\Http\Requests ;
    use Illuminate\Support\Facades\Input;
    use Illuminate\Support\Facades\Redirect;
    
        public function processForm() {
            $lastName  = Input::get('lastName') ;
            $firstName = Input::get('firstName') ;
            return Redirect::to('people/'.$lastName.'/'.$firstName) ;
        }
        public function show($lastName,$firstName) {
            $qry = 'SELECT * FROM tableFoo WHERE LastName LIKE "'.$lastName.'" AND GivenNames LIKE "'.$firstName.'%" ' ;
            $ppl = DB::select($qry);
            return view('people.show', ['ppl' => $ppl] ) ;
        }
    

    people/show.blade.php

    <form method="post" action="/people">
        <input type="text" name="firstName" placeholder="First name">
        <input type="text" name="lastName" placeholder="Last name">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <input type="submit" value="Search">
    </form>
    

    Notes:
    I needed to pass two input fields into the URI.
    I'm not using Eloquent yet, if you are, adjust the database logic accordingly.
    And I'm not done securing the user entered data, so chill.
    Pay attention to the "_token" hidden form field and all the "use" includes, they are needed.

    PS: Here's another syntax that seems to work, and does not need the

    use Illuminate\Support\Facades\Input;
    

    .

    public function processForm(Request $request) {
        $lastName  = addslashes($request->lastName) ;
        $firstName = addslashes($request->firstName) ;
        //add more logic to validate and secure user entered data before turning it loose in a query
        return Redirect::to('people/'.$lastName.'/'.$firstName) ;
    }
    
    0 讨论(0)
  • 2020-11-30 02:39

    Alternatively, if you want to specify expected parameters in action signature, but pass them as arbitrary GET arguments. Use filters, for example:

    Create a route without parameters:

    $Route::get('/history', ['uses'=>'ExampleController@history']);
    

    Specify action with two parameters and attach the filter:

    class ExampleController extends BaseController
    {
        public function __construct($browser)
        {
            $this->beforeFilter('filterDates', array(
                'only' => array('history')
            ));
        }
    
        public function history($fromDate, $toDate)
        {
            /* ... */
        }
    
    }
    

    Filter that translates GET into action's arguments :

    Route::filter('filterDates', function($route, Request $request) {
        $notSpecified = '_';
    
        $fromDate = $request->get('fromDate', $notSpecified);
        $toDate = $request->get('toDate', $notSpecified);
    
        $route->setParameter('fromDate', $fromDate);
        $route->setParameter('toDate', $toDate);
    });
    
    0 讨论(0)
提交回复
热议问题