更新Composer
composer self-update
composer -V
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
Laravel安装
composer create-project --prefer-dist laravel/laravel blog
cd blog/
composer require laravel/ui
php artisan serve --port=8080
http://127.0.0.1:8080/
生成Home控制器
php artisan make:controller HomeController
配置路由 blog\app\routes\web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
//新增条目
Route::get('hello',function(){
return 'hello,Laravel8';
});
访问路由
http://localhost/hello
修改HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
//
public function index()
{
return "index...";
}
}
修改路由
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
//新增条目
Route::get('hello',function(){
return 'hello,Laravel8';
});
Route::get('/home', [HomeController::class, 'index']);
访问路由
http://localhost/home
配置Nginx伪静态
location / {
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break; }
}
初始化bootstrap
php artisan ui bootstrap
npm install
应用
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>首页</title>
<link rel="stylesheet" type="text/css" href="css/app.css">
<script type="text/javascript" src="js/app.js"></script>
</head>
<body>
<h2>首页</h2>
<div>
<!-- 标准的按钮 -->
<button type="button" class="btn btn-default">默认按钮</button>
<!-- 提供额外的视觉效果,标识一组按钮中的原始动作 -->
<button type="button" class="btn btn-primary">原始按钮</button>
<!-- 表示一个成功的或积极的动作 -->
<button type="button" class="btn btn-success">成功按钮</button>
<!-- 信息警告消息的上下文按钮 -->
<button type="button" class="btn btn-info">信息按钮</button>
<!-- 表示应谨慎采取的动作 -->
<button type="button" class="btn btn-warning">警告按钮</button>
<!-- 表示一个危险的或潜在的负面动作 -->
<button type="button" class="btn btn-danger">危险按钮</button>
<!-- 并不强调是一个按钮,看起来像一个链接,但同时保持按钮的行为 -->
<button type="button" class="btn btn-link">链接按钮</button>
</div>
<br>
<nav aria-label="...">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">«</a></li><br>
<li class="page-item"><a class="page-link" href="#">1</a></li><br>
<li class="page-item"><a class="page-link" href="#">2</a></li><br>
<li class="page-item"><a class="page-link" href="#">3</a></li><br>
<li class="page-item"><a class="page-link" href="#">4</a></li><br>
<li class="page-item"><a class="page-link" href="#">5</a></li><br>
<li class="page-item"><a class="page-link" href="#">»</a></li>
</ul>
</nav><br>
</body>
配置AppServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Pagination\Paginator;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//Laravel 更改了默认数据库字符集,为utf8mb4,其中包括对存储表情符号的支持。 如果数据库运行的是MySQL v5.7.7及更高版本,则没有问题;对于那些运行MariaDB或更旧版本的MySQL的用户,在尝试运行迁移时可能会遇到这个错误。
Schema::defaultStringLength(191);
Paginator::useBootstrap();
}
}
Schema::defaultStringLength(191);
数据迁移
php artisan migrate
php artisan migrate:install
php artisan make:migration create_post_table
blog---->database---->migrations
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title',255)->default("");
$table->text('content');
$table->integer('user_id')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
查询帮助
php artisan help migrate
生成 App Key
php artisan key:generate
生成控制器
php artisan make:controller
生成模型
php artisan make:model
生成授权策略
php artisan make:policy
生成 Seeder 文件
php artisan make:seeder
回滚迁移
php artisan migrate:rollback
重置数据库
php artisan migrate:refresh
填充数据库
php artisan db:seed
查看路由列表
php artisan route:list
生成factory
php artisan make:factory PostFactory
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class PostFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->sentence(6),
'content' => $this->faker->paragraph(10),
];
}
}
进入 tinker 环境
php artisan tinker
App\Models\Post::factory()->count(5)->create()
安装laravel-admin
composer global require laravel/installer
laravel new laravel-admin
cd laravel-admin
chmod -R 777 storage/
composer require encore/laravel-admin:1.*
运行下面的命令来发布资源
php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider"
该命令会生成配置文件config/admin.php
,可以在里面修改安装的地址、数据库连接、以及表名,建议都是用默认配置不修改。
修改文件:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Schema;//add fixed sql
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
//
Schema::defaultStringLength(191); //add fixed sql
}
}
然后运行下面的命令完成安装:
php artisan admin:install
启动服务后,在浏览器打开 http://localhost/admin/ ,使用用户名 admin
和密码 admin
登录
安装完成之后,laravel-admin
所有的配置都在config/admin.php
文件中
安装完成之后,后台的安装目录为app/Admin
,之后大部分的后台开发编码工作都是在这个目录下进行
app/Admin/routes.php
文件用来配置后台路由。
app/Admin/bootstrap.php
是laravel-admin
的启动文件, 使用方法请参考文件里面的注释.
app/Admin/Controllers
目录用来存放后台控制器文件,该目录下的HomeController.php
文件是后台首页的显示控制器,ExampleController.php
为实例文件。
后台所需的前端静态文件在/public/vendor/laravel-admin
目录下.