mercredi 22 février 2017

Refactor Laravel Routes

I am having partial success refactoring common laravel routes to a class.

Here is the basic pattern I am refactoring. I will need the same pattern for vendors, users, terminals etc

Route::get('vendors/', 'VendorController@index');
Route::get('vendors/create', 'VendorController@create');
Route::get('vendors/{id}', 'VendorController@show');
Route::post('vendors/search', 'VendorController@search');
Route::put('vendors/', 'VendorController@update');
Route::delete('vendors/', 'VendorController@destroy');

To refactor I made a class that looks like:

class CIRoutes
{
    public static function addRoutes($name)
    {
        $n2 = ucfirst($name);
        Route::get($name.'s/', $n2.'Controller@index');
        Route::get($name.'s/create', $n2.'Controller@create');
        Route::get($name.'s/{id}', $n2.'Controller@show');
        Route::post($name.'s/search', $n2.'Controller@search');
        Route::put($name.'s/', $n2.'Controller@update');
        Route::delete($name.'s/', $n2.'Controller@destroy');
    }
}

Then I create the route:

CIRoutes::addRoutes('vendors');

I thought this was working fine, but I was mistaken. Currently php artisan route:list comes back with Class App\Http\Controllers\RolesController does not exist which it does - i mean it works fine without the refactor.

How can I refactor the common route pattern?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mcKWzr
via IFTTT

Aucun commentaire:

Enregistrer un commentaire