简体   繁体   中英

Laravel: How do I register a route at runtime?

I have a Laravel application with a StandardModel and StandardConnector trait that I'm applying to the vast majority of my Models and Connectors.

By including Route::resource in the routes file, these have functions for all the standard routes and work perfectly, so /invoices shows the index page, /invoices/1/edit shows the standard edit page, etc.

I want to build a route of /invoices/uploadcsv to make standard functionality - can I override Route::resource in some way so that every scaffolded model and controller immediately has this route, and it passes to the uploadCSV function of the Controller?

Thanks,

I want to just give below method as an option to dynamically change route targets conditionally.

<?php 


Route::post('{model}/upload', function($model){

    switch ($model) {
        case 'foo':
            return App::call('\App\Http\Controllers\FooController@uploadCsv');

        case 'bar':
            return App::call('\App\Http\Controllers\BarController@uploadCsv');

        default:
            abort(404);
    }
});

Route::get('{model}/show-upload', function($model){

    switch ($model) {
        case 'foo':
            return App::call('\App\Http\Controllers\FooController@showUpload');

        case 'bar':
            return App::call('\App\Http\Controllers\BarController@showUpload');

        default:
            abort(404);
    }
});

See the file named \\Illuminate\\Routing\\Router . You will get all resource methods.

You can try to create your own Router class and implements those classes.

Also add your own uploadCSV function.

Or

Just add a route to that method separately, before you register the resource:

Route::get('upload', 'FooController@uploadCSV');
Route::resource('foo', 'FooController');

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