Ardy Febriansyah
Ardy Febriansyah

Reputation: 768

Use validation class outside route laravel

I have web app, the app 50% use ajax for making request

And I want to combine ajax routes into one. Example :

I have routes like this

Route::prefix('post')
                 ->middleware(['verify_origin', 'only_ajax'])
                 ->name('post.')
                 ->group(function() {

                    Route::post('save-user-profile', 'User\ProfileSettingController@updateProfile')->name('saveUserProfile');
                    Route::post('save-user-social-media', 'User\ProfileSettingController@updateSocialMedia')->name('saveUserSocialMedia');
                    Route::post('save-user-avatar', 'User\ProfileSettingController@updateAvatar')->name('saveUserAvatar');
                    Route::post('save-user-account', 'User\AccountSettingController@updateAccount')->name('saveUserAccount');

            });         

I want to wrap, all method on post prefix to one controller

Like this

function handlePost($method) {
   call_user_func($method);
}

Usage : handlePost('saveUserProfile')

Above, the method use call action saveUserProfile, the method saveUserProfile using validation class.

How to call method, but still use validation class

Upvotes: 2

Views: 238

Answers (1)

Michael
Michael

Reputation: 1076

You can have 1 route for instance save-user that might be a single controller class:

Route::prefix('post')
             ->middleware(['verify_origin', 'only_ajax'])
             ->name('post.')
             ->group(function() {

                Route::post('save-user', 'User\ProfileSettingController')->name('saveUserProfile');

        });

Then on the __invoke method inside the controller determine what kind of action needs to be triggered (probably will be passed by an action attribute etc...) and call the appropriate method.

Upvotes: 1

Related Questions