Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 8 array callable syntax for 'uses' in route not working

Route::post('/update-client', 
    array(
        'uses' =>  'Client\API\ClientAPIController@Update', 
        'as'   =>  'apiUpdateClient',
    )
);

Route::post('/delete-client', 
    array(
        'uses' =>  'Client\API\ClientAPIController@Delete', 
        'as'   =>  'apiDeleteClient',
    )
);

But, these routes are not now working in Laravel 8. Below are error details

Target class [Client\API\ClientAPIController] does not exist.

It works if I write like below and got it fixed

Route::post('/update-client', 
    array(
        'uses' =>  'App\Http\Controller\Client\API\ClientAPIController@Update', 
        'as'   =>  'apiUpdateClient',
    )
);

Question -

My route file contains many client routes. So, instead of appending App\Http\Controller with each route, I thought to do it like below,

use App\Http\Controllers\Client\API\ClientAPIController;

Route::post('/update-client', 
    array(
        'uses' =>  [ClientAPIController::class, 'Update'], 
        'as'   =>  'apiUpdateClient',
    )
);

Above code gave me error,,,

ReflectionFunction::__construct() expects parameter 1 to be string, array given

Am I using wrong syntax for uses parameter?

like image 483
Pankaj Avatar asked Sep 21 '20 15:09

Pankaj


2 Answers

Just remove the uses as told in laravel documentation.

use App\Http\Controllers\Client\API\ClientAPIController;

Route::get('/users', [ClientAPIController::class, 'Update']);

refer to this documentation https://laravel.com/docs/8.x/upgrade

All of a sudden they changed the way we write laravel routes

like image 92
Deepesh Thapa Avatar answered Oct 16 '22 15:10

Deepesh Thapa


You have the use the new way:


Route::post(
    '/update-client',
    [ClientAPIController::class, 'update']
)->name('apiUpdateClient');

Route::post(
    '/delete-client',
    [ClientAPIController::class, 'delete']
)->name('apiDeleteClient');

More info: https://laravel.com/docs/8.x/routing#named-routes

like image 38
Russ_AB Avatar answered Oct 16 '22 16:10

Russ_AB