创建数据库
创建名为laravel_db的新数据库。在这个数据库中,创建新的表,命名为产品,如下所示。
产品表的结构
产品表的数据
安装Laravel
-
从getcomposer.org/download/下载并安装最新版本的Composer.
-
在Visual Studio Code中打开Windows终端,用下面的命令安装Laravel安装程序。
composer global require laravel/installer
-
创建新的文件夹,命名为LearnLaravelWithRealApps.使用Visual Studio Code打开LearnLaravelWithRealApps文件夹.在这个文件夹中,创建新的项目,名为LearnLaravelWithRealApps,命令如下。
laravel new LearnLaravelWithRealApps
-
运行LearnLaravelWithRealApps项目,命令如下。
php artisan serve
-
打开LearnLaravelWithRealApps项目,网址如下。
http://localhost:8000
连接到数据库
打开根目录下的.env文件.添加如下值,连接到数据库。
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=
清除配置缓存
在Visual Studio Code中打开Windows终端,用以下命令清除配置缓存。
php artisan config:clear
创建产品模型
在app/Models文件夹下创建名为Product.php的新PHP文件,如下所示。
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $primarykey = 'id';
protected $table = 'product';
public $timestamps = false;
}
创建ProductRepository接口
创建名为Repositories的新文件夹。在Repositories文件夹中,创建名为Product的新文件夹。在app/Repositories/Product文件夹下创建名为ProductRepository.php的新PHP文件,如下所示。
<?php
namespace App\Repositories\Product;
interface ProductRepository
{
public function findAll($n);
}
创建ProductRepositoryImpl类
在app/Repositories/Product文件夹下创建名为ProductRepositoryImpl.php的新PHP文件,如下所示。
<?php
namespace App\Repositories\Product;
use App\Models\Product;
class ProductRepositoryImpl implements ProductRepository
{
public function findAll($n)
{
$products = Product::paginate($n);
$products->setPath('/demo/index');
return $products;
}
}
声明产品仓库
打开app/Providers文件夹下的AppServiceProvider.php文件,添加声明新的资源库到注册方法,如下所示。
<?php
namespace App\Providers;
use App\Repositories\Product\ProductRepository;
use App\Repositories\Product\ProductRepositoryImpl;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(ProductRepository::class, ProductRepositoryImpl::class);
}
public function boot()
{
}
}
安装Bootstrap分页
打开app/Providers文件夹下的AppServiceProvider.php文件,添加声明Bootstrap的分页方法,如下所示。
<?php
namespace App\Providers;
use App\Repositories\Product\ProductRepository;
use App\Repositories\Product\ProductRepositoryImpl;
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(ProductRepository::class, ProductRepositoryImpl::class);
}
public function boot()
{
Paginator::useBootstrap();
}
}
控制器
在app/Http/Controllers文件夹下创建名为DemoController.php的新PHP文件,如下所示。
<?php
namespace App\Http\Controllers;
use App\Repositories\Product\ProductRepository;
class DemoController extends Controller
{
protected $productRepository;
public function __construct(ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
}
public function index()
{
$data = array(
'products' => $this->productRepository->findAll(2)
);
return view('demo/index')->with($data);
}
}
观点
在resources/views文件夹下创建名为demo的新文件夹。在这个文件夹中,创建名为index.blade.php的新刀片文件,如下所示。
<html>
<head>
<title>Laravel</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col"> </div>
</div>
<div class="row">
<div class="col">
<table border="1" class="table">
<tr>
<th>Id</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Status</th>
<th>Created</th>
<th>Description</th>
</tr>
@foreach($products as $product)
<tr>
<td>{{$product->id}}</td>
<td>{{$product->name}}</td>
<td>{{$product->price}}</td>
<td>{{$product->quantity}}</td>
<td>{{$product->status}}</td>
<td>{{$product->created}}</td>
<td>{{$product->description}}</td>
</tr>
@endforeach
<tr>
<td colspan="3" align="center">
<div class="d-flex justify-content-center">
@if ($products->hasPages())
Showing {{((($products->currentPage() -1) * $products->perPage()) + 1)}} to {{((($products->currentPage() -1) * $products->perPage()) + $products->count()) }} of {{ $products->total() }} products. Page {{ $products->currentPage() }}/{{ $products->lastPage() }}
@endif
</div>
</td>
<td colspan="4" align="center">
<div class="d-flex justify-content-center">
{{ $products->links() }}
</div>
</td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
路由
打开routes文件夹中的web.php文件, 添加新的路由,如下所示:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;
Route::get('/', [DemoController::class, 'index']);
Route::get('/demo', [DemoController::class, 'index']);
Route::get('/demo/index', [DemoController::class, 'index']);
Laravel项目的结构
运行应用程序
在Demo控制器中访问index动作,其URL如下。
输出