提交 f10f9e6a 编写于 作者: 彭世瑜's avatar 彭世瑜

fix

上级 65f46a25
【李炎恢】【Laravel / 核心篇 / PHP框架 / 阶段一】: https://www.bilibili.com/video/BV1gE411j78F
讲义代码:
- 链接:https://pan.baidu.com/s/1Y_o3RdUEsrj6tHchMwY3Mg
- 提取码:f4qz
[安装Laravel编程环境](blog/laravel/install-php.md)
[Route路由](blog/laravel/route.md)
\ No newline at end of file
# 安装Laravel编程环境
测试环境:
```
os macOS Mojave 10.14.4
PHP 8.0.20
laravel 9.x
```
## 1、安装PHP
php源码:[https://github.com/php/php-src](https://github.com/php/php-src)
因为使用macOS的包管理工具HomeBrew没有成功安装PHP,故采用源码编译安装的方式
下载地址:[https://www.php.net/downloads](https://www.php.net/downloads)
```bash
# 下载
wget https://www.php.net/distributions/php-8.0.20.tar.gz
# 解压
tar -zxvf php-8.0.20.tar.gz
cd php-8.0.20
# 配置
./configure \
--prefix=/Users/user/Applications/php/8.0.20 \
--with-config-file-path=/Users/user/Applications/php/8.0.20/etc \
--with-curl \
--with-openssl \
--with-mysqli \
--with-pdo-mysql \
--with-iconv \
--with-mhash \
--with-zlib \
--enable-mbstring \
--enable-gd \
--enable-gd-jis-conv \
--enable-sockets \
--enable-fpm \
--enable-xml \
--enable-pdo \
--enable-cli \
--enable-pcntl \
--enable-soap \
--enable-opcache \
--enable-fileinfo \
--disable-rpath \
--enable-mysqlnd \
--with-zip \
--enable-simplexml \
--with-libxml \
--with-sqlite3 \
--with-pdo-sqlite \
--enable-phar \
--enable-tokenizer \
--enable-cgi
# 编译
make
# 安装
make install
# 添加软连接
ln -s /Users/user/Applications/php/8.0.20/bin/php /usr/local/bin/php8
# 查看PHP的版本
php8 -v
PHP 8.0.20 (cli) (built: Jun 26 2022 18:43:20) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.20, Copyright (c) Zend Technologies
# 本机的安装地址php8,由于本机装有有多个PHP版本,故取了别名
/usr/local/bin/php8 -> /Users/user/Applications/php/8.0.20/bin/php
```
### 安装PHP遇到的问题及解决
如果提示部分依赖缺失,需要安装
```bash
# 检查是否安装
brew info openssl
# 搜索可安装的软件包
brew search openssl
# 安装
brew install openssl
```
按照提示配置 pkgconfig、openssl、libiconv环境变量
```bash
# ~/.bash_profile
# pkgconfig
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
# openssl@1
export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig:$PKG_CONFIG_PATH"
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
# libiconv
export PATH="/usr/local/opt/libiconv/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/libiconv/lib"
export CPPFLAGS="-I/usr/local/opt/libiconv/include"
```
> 提示:如果配置好环境变量,重启会话窗口还没没有编译通过,
可以尝试把当前解压的编译文件夹整个删除,再重新解压一份新的
## 2、安装composer
地址:[https://getcomposer.org/download/](https://getcomposer.org/download/)
[https://github.com/composer/composer](https://github.com/composer/composer)
```bash
# 下载composer
php8 -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php8 composer-setup.php
php8 -r "unlink('composer-setup.php');"
# 移入对应PHP版本的可执行目录
mv composer.phar /Users/user/Applications/php/8.0.20/bin/composer
```
将composer8取个别名
```bash
# ~/.bash_profile
# php8 注意等号= 两边不要有空格
alias composer8="php8 /Users/user/Applications/php/8.0.20/bin/composer"
```
镜像
```bash
# 使用阿里云镜像
composer8 config -g repo.packagist composer https://mirrors.aliyun.com/composer/
# 检验版本
composer8 -V
Composer version 2.3.7 2022-06-06 16:43:28
```
## 3、创建laravel项目
中文文档:https://learnku.com/docs/laravel/9.x
```bash
composer8 create-project laravel/laravel laravel-app
```
PHPStrom 安装插件 laravel
启动服务
```bash
php8 artisan serve
```
\ No newline at end of file
# Route路由
## 路由配置
```php
// routes/web.php
# 接收get请求 http://127.0.0.1:8000/test
Route::get('/test', function () {
return 'Hello Laravel';
});
# any接收任何请求方式
Route::any('/test', function () {
return 'Hello Laravel';
});
# 指定提交方式
Route::match(['get', 'post'], '/test', function () {
return 'Hello Laravel';
});
# 接收参数 http://127.0.0.1:8000/test/5
Route::get('/test/{id}', function ($id) {
return $id;
});
```
## 控制器
```bash
php8 artisan make:controller TaskController
```
自动生成的模板代码
```php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TaskController extends Controller
{
//
}
```
编写业务代码
```php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TaskController extends Controller
{
public function index()
{
return 'task';
}
public function read($id)
{
return $id;
}
}
```
路由配置
```php
// routes/web.php
<?php
use App\Http\Controllers\TaskController;
use Illuminate\Support\Facades\Route;
Route::get('/task', [TaskController::class, 'index']);
Route::get('/task/read/{id}', [TaskController::class, 'read']);
```
## 路由参数
参数约束
```php
// 允许:http://127.0.0.1:8000/user/5
// 报错:http://127.0.0.1:8000/user/ooxx
// 单个参数
Route::get('/user/{id}', function ($id) {
return $id;
})->where('id', '[0-9]+');
// 多个参数
Route::get('/user/{id}/{name}', function ($id, $name) {
//
})->where([
'id' => '[0-9]+',
'name' => '[a-z]+'
]);
```
全局约束
```php
// App\Providers\RouteServiceProvider
<?php
namespace App\Providers;
class RouteServiceProvider extends ServiceProvider
{
public function boot()
{
// 添加全局约束
Route::pattern('id', '[0-9]+');
}
}
```
局部约束
```php
Route::get('/user/{id}', function ($id) {
return $id;
})->where('id', '.*');
```
## 重定向路由
```php
// 状态码默认302,临时重定向
Route::redirect('/index', '/task');
// 301 永久重定向
Route::redirect('/index', '/task', 301);
// 等价于 永久重定向
Route::permanentRedirect('/index', '/task');
```
## 视图路由
视图模板
```html
{{-- resources/views/task.blade.php --}}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{{-- 变量 --}}
{{$name}}
</body>
</html>
```
> !+ tab 可快速创建HTML模板
```php
// http://127.0.0.1:8000/task
// 方式一:
Route::view('/task', 'task', [
'name'=> 'Tom'
]);
// 方式二:使用助手函数返回view
Route::get('/task', function () {
return view('task', [
'name' => 'Tom'
]);
});
// 方式三:
Route::get('/task', [TaskController::class, 'index']);
```
Controller返回view
```php
<?php
namespace App\Http\Controllers;
class TaskController extends Controller
{
public function index()
{
return view('task', [
'name' => 'Tom'
]);
}
}
```
https://www.bilibili.com/video/BV1gE411j78F?p=4&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
......@@ -4,6 +4,8 @@
[笔记:PHP 零基础入门到精通教程(P2 mysql 数据库 5 天)](blog/php-mysql/index.md)
[Laravel](blog/laravel/index.md)
## 文章
[PHP 手册](https://www.php.net/manual/zh/index.php)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册