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

后端 未结 7 916
余生分开走
余生分开走 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: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);
    });
    

提交回复
热议问题