Peace be upon you, and Allah mercy and blessings
Hello everyone, laravel 8 has now been released, as we know there are some changes in laravel 8, one of which is writing routes, because in laravel 8 currently the default namespace is null, so we can no longer use strings to write routes, thing this might make some people not familiar with writing routes with laravel 8 .
So on this occasion, we will change the route writing to be like Laravel 7 and below. First of all let’s create a new project first, and make sure your laravel installer is on version 4, because laravel 8 is now using installer version 4, you can run the command below to update the installer.
composer global update laravel/installer
if the above command fails you can directly force download with the command
composer global require "laravel/installer:^4.0"
Then we can create the project with the command
laravel new laravel_8
After that we will do the Laravel download process, and what’s new is that we are greeted with Laravel writing as shown below
Wait for the installation process to complete, it will appear like this
then we open it in our text editor, I will use vs code, and type this command in terminal
cd laravel_8
code .
and we first create a controller with the name HomeController, run the command
php artisan make:controller HomeController
Then we open the controller that we created earlier and add an index function that returns a string
public function index(){
return 'Hello Home';
}
Later we will call this function on the route, now we open route/web.php and type
Route::get('/home', 'HomeController@index');
And we open our project in the browser with the following command in the terminal
php artisan serve
Then a display like the one below will appear
The initial display that changed in laravel 8, now we will go to the url: http://127.0.0.1:8000/home, an error will appear
This happens because the writing of the route in laravel 8 is different from the previous version, now we open route/web.php again and change the syntax to
use App\Http\Controllers\HomeController;Route::get('/home', [HomeController::class, 'index']);
Then we go back to the browser and refresh then the display will be like this
The route write change is caused by the namespace on the default route being null. Now, we don’t want to write the latest route, because it’s not good, how do we change it, we can open Providers/RouteServiceProvider, then change it
protected $namespace = null;
Become
protected $namespace = 'App\Http\Controllers';
And
Route::middleware('web')
->group(base_path('routes/web.php'));Route::prefix('api')
->middleware('api')
->group(base_path('routes/api.php'));
Become
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
And we change the writing on the route
Route::get('/home', 'HomeController@index');
Then in the browser the results will be the same
Maybe that’s all the discussion from me
Thank you :)