2
\$\begingroup\$

I have below route. I am using Laravel 8.

Route::post('/update-crew', 
 array(
 'uses' => 'App\Http\Controllers\Directory\Crew\API\CrewAPIController@Update', 
 'as' => 'apiUpdateCrew',
 )
);

Is there any way to use namespace instead of writing the path in string. My route contains both uses and as.

can I improve the code by keeping the uses and as and instead of string path, can I use namespace something like below?

use App\Http\Controllers\Directory\Crew\API\CrewAPIController;
Route::post('/update-crew', 
 array(
 'uses' => ['CrewAPIController', 'Update'], 
 'as' => 'apiUpdateCrew',
 )
);

Is this possible?

Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
asked Sep 29, 2020 at 5:45
\$\endgroup\$
1
  • \$\begingroup\$ I rolled back your last edit. After getting an answer you are not allowed to change your code anymore. This is to ensure that answers do not get invalidated and have to hit a moving target. If you have changed your code you can either post it as an answer (if it would constitute a code review) or ask a new question with your changed code (linking back to this one as reference). See the section What should I not do? on What should I do when someone answers my question? for more information \$\endgroup\$ Commented Sep 30, 2020 at 23:58

1 Answer 1

1
\$\begingroup\$

I recently heard about Laravel 8 changing the way routing was declared, as this video explains that developers upgrading from Laravel 7 or earlier will need to either switch to using the PHP callable syntax or fully namespace the controller in the string syntax (as your first code snippet does) (see the section Routing under Upgrading To 8.0 From 7.x).

Yes adding that use statement at the top then you should be able to reference the class without the namespace before it.

You can use the ::class keyword for name resolution instead of using a string literal:

'uses' => [CrewAPIController::class, 'Update'], 

Many populate IDEs will index the class names and allow you to then easily jump to the class definition -e.g. ctrl/command + click on name).


I also wasn't familiar with the 'as' syntax, then I found this comment on this accepted SO answer to What does "as" keyword mean in Laravel routing?

As keyword is in older version, if you change the documentation to 5.2 you can see the as keyword. In newer version it's ->name

So you could use the name() method to make the Named route instead:

Route::post('/update-crew', [CrewAPIController::class, 'Update'])
 ->name('apiUpdateCrew');
answered Sep 29, 2020 at 15:31
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.