Basic use of laravel

laravel framework

中文文档地址:
Laravel8 Chinese Documentation


@TOC


1. Installation

Install Laravel

使用 Composer 安装 Laravel 安装器:

composer global require laravel/installer

composer install

composer下载地址:
https://www.phpcomposer.com/

集成开发环境安装composer
Double-click the installation package to start the installation, this is the installation interface:
insert image description here
keep going. Up to this step, select the location of PHP.exe in your integrated development environment, click to select, and then continue the installation:
insert image description here
insert image description here
confirm that there is no error and continue the installation. After the installation is complete, enter in the command prompt: The following figure appears on
composer to indicate that it is correct; install the dependency package :

insert image description here

composer global require laravel/installer

create project

laravel new 项目名

Run the project
command prompt to enter the root directory of the project and execute the command:
php artisan serve
insert image description here
insert image description here

2. Framework directory structure

insert image description here

2. Routing

路由文件是在routes文件下的web.php文件

1. Basic use

grammar

Use a controller to handle:

//第一个参数是访问地址,
//第二个参数是使用处理这个请求的类,
//第三个参数是类下面的方法名
Route::get('index',[\App\Http\Controllers\UserController::class,'index']);

Use an anonymous function to handle:

Route::get('/', function () {
    
    
});

Laravel allows you to register routes that respond to any HTTP request:

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

Need to register a route that can respond to multiple HTTP requests:

//第一个传入请求方式
Route::match(['get', 'post'], '/', function () {
    
    
   
});
Route::any('/', function () {
    
    

});

optional parameters

//路由可选参数
Route::get('bixuan2/{id?}',function ($id="默默人"){
    
    
    return "可选参数".$id;
})->name("bi");

Required parameters

//路由必传参数
Route::get('bixuan/{id}',function ($id){
    
    
    return "必选参数".$id;
});

routing packet

// 路由分组
Route::prefix('admin')->group(function (){
    
    
   Route::get('index',function (){
    
    
       return "这是admin下面的index页面";
   })->name('index');

    Route::get('login',function (){
    
    
        return "这是admin下面的login页面";
    })->name('login');
});

You can alias the route, and then use the command to view the route information

Command to view all routing information

php artisan route:list
//路由可选参数
Route::get('bixuan2/{id?}',function ($id="默默人"){
    
    
    return "可选参数".$id;
})->name("bi");

insert image description here

3. Controller

创建控制器命令:

//php artisan make:controller 控制器名
php artisan make:controller IndexController

Command summary:

Create controller command:

//php artisan make:controller 控制器名
php artisan make:controller IndexController

Guess you like

Origin blog.csdn.net/qq_48082548/article/details/129200443