With the development of network technology, Web applications have become an indispensable part of people's lives. In the development process of web applications, the PHP language is one of the most popular programming languages, and the Laravel framework is one of the leading, best, and most popular frameworks in the development process of PHP web applications.
The Laravel framework emerged to simplify common problems in the PHP web application development process, such as data query, form validation, security and other issues. This article will introduce the use of Laravel9 framework in detail.
1. Installation of Laravel9
First of all, in order to use Laravel9 in the project, we need to install it. We can install it using the Composer installation script officially provided by Laravel. Open the terminal and enter the following command to install:
composer create-project --prefer-dist laravel/laravel myproject
Here, myproject
represents the name of your project.
2. Configuration of Laravel9
After successful installation, we need to perform some configurations on Laravel. First open the .env.example
file in the project folder and rename it to .env
. This file saves the basic configuration information of our project, such as database configuration, application key, email configuration, etc.
Modify the database configuration parameters in the .env
file so that Laravel can connect to our database. The specific configuration method is as follows:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=123456
In addition to database and email configuration,# There are other configuration variables in the ##.env file, which you can modify as needed.
routes/web.php file in the project to start defining our routes.
Route::get('/', function () { return view('welcome'); });
welcome view will be displayed. Among them, we use the
Route::get() method to respond to the GET request and configure routing.
namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller { public function index() { $users = User::all(); return view('users.index', compact('users')); } }
User model, and then return it to the view for display through the index method.
resources/views directory. This is where we write all the HTML, CSS, and JavaScript code for our application.
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Dashboard</div> <div class="card-body"> @if (session('status')) <div class="alert alert-success" role="alert"> {{ session('status') }} </div> @endif You are logged in! </div> </div> </div> </div> </div> @endsection
resources/views/home.blade.php.
@extends is used in the view file to inherit the header and sidebar views. In
@section('content'), we define the HTML content of the main part. In the view file, we can use the Blade template engine provided by Laravel to achieve a more flexible rendering method.
app/Http/Controllers directory and are created through the artisan command line tool:
php artisan make:controller UserController --resource
UserController and allows us to access it via routing.
namespace TestsUnit; use TestsTestCase; use IlluminateFoundationTestingRefreshDatabase; use AppModelsUser; class UserTest extends TestCase { use RefreshDatabase; /** @test **/ public function a_user_can_have_a_name() { $user = factory(User::class)->create(['name' => 'John']); $this->assertEquals('John', $user->name); } }
factory() method, we created a user with the default name "John" and compared whether the user's name was returned correctly.
The above is the detailed content of How to use Laravel9 framework in php?. For more information, please follow other related articles on the PHP Chinese website!