简体   繁体   中英

resource controller, pass multiple parameters using AJAX

I am using Laravel<\/code> for the first time to create an API to be accessed using AJAX from an angular.js single page app. I can't figure out how to configure controller and URL to pass more than one argument to any of the methods

Route::group(array('prefix' => 'api/v1'), function(){
    Route::resource('event', 'EventController');    
});

You can add that specific route above the resource (I'm assuming you are using GET for your ajax requests):

Route::group(array('prefix' => 'api/v1'), function(){
    Route::get('event/{start}/{end}', 'EventController@index');
    Route::resource('event', 'EventController');    
});

In your controller, make your parameters optional so you can use the same controller action for both routes, api/v1/event and api/v1/event :

<?php

class EventController extends BaseController {

    public function index($start = null, $end = null)
    {
        if (isset($start) && isset($end)) {
            return $this->eventsRepository->byDate($start, $end);
        }

        return $this->eventsRepository->all();
    }

}

If you want to be more specific about the start and end wildcards format, you can use where:

Route::get('event/{start}/{end}', 'EventController@index')
         ->where([
            'start' => 'regexp-here', 
            'end' => 'regexp-here'
           ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM