composer
先切换镜像源吧,不然下不下来laravel框架.1
composer config -g repo.packagist composer https://packagist.phpcomposer.com
然后进入网站根目录安装laravel框架1
composer create-project laravel/laravel laravel v5.8.17
这里因为我安装的是phpstudy,所以直接访问http://127.0.0.1/laravel/public/
即可.
也可以cmd进入到public目录,php -S 0.0.0.0:9999
,然后访问127.0.0.1:9999
路由
建立url和程序之间的映射.
路由文件
5.8版本的路由文件在routes->web.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
|--------------------------------------------------------------------------
| 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');
});
各种路由
- get路由
1
2
3Route::get('chenxiyuan',function(){
return 'Hello world';
});
访问http://127.0.0.1/laravel/public/chenxiyuan
,成功输出Hello world.
post路由
1
2
3Route::post('post',function(){
return 'post';
});match路由
1
2
3Route::match(['get','post'],'match',function(){
return 'match路由,匹配多种';
});any路由
1
2
3Route::any('any',function(){
return '任意方法';
});重定向路由
1
2Route::redirect('about','chenxiyuan');
Route::redirect('about','http://www.chenxiyuan.vip');
访问http://127.0.0.1/laravel/public/about跳转到http://127.0.0.1/laravel/public/chenxiyuan
- 视图路由
1
2
3Route::get('/', function () {
return view('welcome');
});
路由传参
必须传参
1 | Route::get('user/{id}',function($id){ |
访问http://127.0.0.1/laravel/public/user/1,输出:`id:1`
可选传参
1 | Route::get('user/{id?}',function($id=null){ |
路由命名
命名路由可以方便地为指定路由生成URL
或者重定向.通过在路由定义上链式调用name
方法指定路由名称:1
2
3Route::get('user/profile', function () {
return route('profile');
})->name('profile');
也可以这样写:1
2
3Route::get('home/about',['as'=>'about',function(){
return route('about');
}]);
访问http://127.0.0.1/laravel/public/home/about,输出`http://127.0.0.1/laravel/public/home/about`.
路由群组
1 | Route::group(['prefix' => 'group'],function(){ |
prefix指定一个前缀,地址需要加上前缀才能访问到.
正则表达式约束
你可以使用路由实例上的where
方法约束路由参数的格式.where方法接受参数名称和定义参数应如何约束的正则表达式:1
2
3
4
5
6
7
8
9
10
11Route::get('user/{name}', function ($name) {
//
})->where('name', '[A-Za-z]+');
Route::get('user/{id}', function ($id) {
//
})->where('id', '[0-9]+');
Route::get('user/{id}/{name}', function ($id, $name) {
//
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
控制器
真正的项目,并不是把函数方法直接写在路由里的,而是在控制器中定义,再和路由进行关联.控制器目录:app->Http->Controllers
在Controllers文件夹下新建一个类MemberController.php
与Controller.php
同目录:1
2
3
4
5
6
7
8
9
10
11
namespace App\Http\Controllers;
class MemberController extends Controller
{
public function info($id)
{
return 'memberinfo:id='.$id;
}
}
在路由中注册:Route::get('memberinfo/{id}','MemberController@info');
访问http://127.0.0.1/laravel/public/memberinfo/100,成功输出:memberinfo:id=100
视图
为方便运行一下public文件夹cmdphp -S 0.0.0.0:9999
视图目录:resources->views
.
新建一个greeting.blade.php
模板1
2
3
4
5<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
新建一个Greeting.php
控制器1
2
3
4
5
6
7
8
9
10
11
12
namespace App\Http\Controllers;
class Greeting extends Controller
{
public function greet()
{
return view('greeting',['name'=>'chenxiyuan']);
}
}
添加到路由:1
Route::get('greet','Greeting@greet');
模型
模型文件放在app文件夹下,新建一个Greet.php
:1
2
3
4
5
6
7
8
9
10
11
namespace App;
use Illuminate\Database\Eloquent\Model;
class Greet extends Model{
public static function getblog(){
return 'chenxiyuan.vip';
}
}
改写Greeting控制器:1
2
3
4
5
6
7
8
9
10
11
namespace App\Http\Controllers;
use App\Greet;
class Greeting extends Controller
{
public function greet(){
return Greet::getblog();
}
}
路由不变,访问http://127.0.0.1:9999/greet,成功输出:chenxiyuan.vip
模板
Blade 是 Laravel 提供的一个简单而又强大的模板引擎。和其他流行的 PHP 模板引擎不同,Blade 并不限制你在视图中使用原生 PHP 代码。所有 Blade 视图文件都将被编译成原生的 PHP 代码并缓存起来,除非它被修改,否则不会重新编译,这就意味着 Blade 基本上不会给你的应用增加任何负担。Blade 视图文件使用 .blade.php 作为文件扩展名,被存放在 resources/views 目录。
模板继承
定义布局
主页面:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<!-- 文件保存于 resources/views/layouts/app.blade.php -->
<html>
<head>
<title>应用程序名称 - @yield('title')</title>
</head>
<body>
@section('sidebar')
这是主布局的侧边栏。
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
@section
命令定义了视图的一部分内容,而@yield
指令是用来显示指定部分的内容.
继承布局
当定义子视图时,你可以使用Blade提供的@extends
命令来为子视图指定应该 「继承」 的布局.继承Blade布局的视图可使用 @section 命令将内容注入于布局的@section
中.而主布局中使用@yield
的地方会显示这些子视图中的@section
间的内容:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<!-- 文件保存于 resources/views/layouts/child.blade.php -->
@extends('layouts.app')
@section('title', 'Page Title')
@section('sidebar')
@parent
<p>这将追加到主布局的侧边栏。</p>
@endsection
@section('content')
<p>这是主体内容。</p>
@endsection
在上面的例子里,@section
中的sidebar使用@parent
命令在主布局的@section('sidebar')
中增加内容(不是覆盖).渲染视图时,@parent
指令会被替换为主布局中@section('sidebar')
间的内容.
流程控制
if语句
你可以使用@if
、@elseif
、@else
及 @endif
指令来构建 if 表达式。这些命令的功能等同于 PHP 中的语法:1
2
3
4
5
6
7@if (count($records) === 1)
我有一条记录!
@elseif (count($records) > 1)
我有多条记录!
@else
我没有任何记录!
@endif
循环
1 | @for ($i = 0; $i < 10; $i++) |
注释
Blade 也能在视图中定义注释。然而,跟 HTML 的注释不同的,Blade 注释不会被包含在应用程序返回的 HTML 内:1
{{-- 此注释将不会出现在渲染后的 HTML --}}
php
在某些情况下,将 PHP 代码嵌入到视图中很有用。你可以使用 Blade 的 @php 指令在模板中执行一段纯 PHP 代码:1
2
3@php
//
@endphp