Laravel框架学习(四)
一、 composer的安装:
1.Composer是什么?
是 PHP 用来管理依赖(dependency)关系的工具。
你可以在自己的项目中声明所依赖的外部工具库(libraries),
Composer 会帮你安装这些依赖的库文件。
2.网址:https://getcomposer.org
下载:https://getcomposer.org/download/
中国全量镜像:http://pkg.phpcomposer.com/
启用本镜像服务命令:
composer config -g repo.packagist composer https://packagist.phpcomposer.com
或
composer config repo.packagist composer https://packagist.phpcomposer.com
3.Composer常用命令:
composer -v 查看版本
composer selfupdate 更新composer
二、安装Laravel框架
文档网站:http://www.golaravel.com/
选择5.1版本:http://www.golaravel.com/laravel/docs/5.1/
对运行环境的要求:
- PHP >= 5.5.9
- OpenSSL PHP 扩展
- PDO PHP 扩展
- Mbstring PHP 扩展
- Tokenizer PHP 扩展
通过 Composer Create-Project 命令安装 Laravel:
命令:composer create-project laravel/laravel --prefer-dist
三、本地域名解析与apapche虚拟主机配置(window下)
1. 打开:C:WindowsSystem32driversetc目录中的hosts文件:
配置信息:127.0.0.1 自定义主机名
2. 在apache的httpd-vhosts.conf配置文件中配置
<VirtualHost *:80>
ServerAdmin zhangtao@lampbrother.net
DocumentRoot "虚拟主机目录位置"
ServerName 虚拟主机名
ErrorLog "logs/虚拟主机名-error.log"
CustomLog "logs/虚拟主机名-access.log" common
</VirtualHost>
四、应用程序结构介绍:
详见手册中《系统架构》的应用程序结构
五、HTTP 路由
1. 基本路由:
Route::get('/', function()
{
return 'Hello World';
});
Route::post('foo/bar', function()
{
return 'Hello World';
});
Route::put('foo/bar', function()
{
//
});
Route::delete('foo/bar', function()
{
//
});
Route::match(['get', 'post'], '/', function() //多种请求注册路由
{
return 'Hello World';
});
2. 路由参数
Route::get('user/{id}', function($id)
{
return 'User '.$id;
});
六、laravelDebug安装与调试命令
网址:https://github.com/barryvdh/laravel-debugbar
安装命令:composer require barryvdh/laravel-debugbar
进入:config/app.php文件
配置:
BarryvdhDebugbarServiceProvider::class,
'Debugbar' => BarryvdhDebugbarFacade::class,
执行命令:php artisan vendor:publish --provider="BarryvdhDebugbarServiceProvider"
七、控制器的创建
创建一个RESTful资源控制器
命令:php artisan make:controller StuController
命令:php artisan make:controller StuController --plain (不好用)
--plain表示创建一个空的控制器
控制器中代码
//在控制器中查询数据,并加载模板输出
public function index()
{
$list = DB::table('stu')->get();
return view('stu.index',["list"=>$list]);
//return view('stu.index', compact('list'));
}
在routes.php的路由文件中配置
Route::get('stu/index', 'StuController@index');
public function index()
{
//$list = DB::table('stu')->get();
$list = DB::table('stu')->paginate(5);
//return view('stu.index',['list'=>[]]);
return view('stu.index',["list"=>$list]);
//return view('stu.index', compact('list'));
}
public function create()
{
return view("stu.create");
}
public function store(Request $request)
{
//dd($request);
$input = $request->all();
unset($input['_token']);
$id = DB::table('stu')->insertGetId($input);
return "添加成功!id号".$id;
}
public function update()
{
return "update";
}
public function show($id)
{
$stu = DB::table('stu')->where("id","=",$id)->first();
dd($stu);
}
public function destroy($id)
{
return "delete".$id;
}
八、 Laravel 中Request请求对象的使用
1. 使用方式:
1.1 通过 Facade
在控制器中使用: use Request导入
在控制器的方法中获取参数信息:$name = Request::input('name');
1.2 通过依赖注入
在控制器中使用:use IlluminateHttpRequest; 导入
在控制器的方法中使用参数注入request对象
public function store(Request $request)
{
$name = $request->input('name');
}
2. 取得输入数据:
2.1 $name = Request::input('name'); 获取请求参数name的值
2.2 $name = Request::input('name', 'Sally'); 获取参数name的值,若没有则使用Sally默认值
2.3 if (Request::has('name')){ ... } 判断是否有此参数。
2.4 Request::all(); 获取所有参数值
2.5 获取部分参数值
$data = $request->only("name","id"); //获取部分参数值
$data = $request->except("name"); //获取指定外部分参数值
2.6 获取数组中的值
九. Laravel中的响应:Response
1. 基本响应
1.1 从路由返回字串
Route::get("/hh",function(){
return "Hello World!";
});
1.2 自定义响应:
在控制器中使用response: use IlluminateHttpResponse;
控制器方法中的代码
$content="Hello Laravel!";
$status = 200;
$value = "text/html";
return (new Response($content, $status))->header('Content-Type', $value);
1.3 在响应送出视图
return response()->view('hello')->header('Content-Type',"text/html");
1.4 附加 Cookies 到响应
return response($content)->withCookie(cookie('name', 'value'));
2. 重定向
2.1 return redirect('user/login');
十 视图
视图被保存在 resources/views 文件夹内
实例创捷一个vv.php视图文件
在控制器的方法中加载视图方式:
1. return view("vv"); //加载视图
2. return view("vv",['name'=>"zhangsan","age"=>20]); //加载视图,并携带参数
3. return view("vv")->with("name","lisi")->with("age",30); //通过with携带参数值
在视图中如何输出
<body>
<h2>Laravel框架视图测试</h2>
姓名:<?php echo $name; ?> 年龄:<?php echo $age; ?>
</body>
十一 模板引擎:--Blade
Blade 模板后缀名都要命名为 .blade.php
模板导入css等文件可以使用{{asset(‘css/bootstrap.min.css’)}}
<link href="/css/bootstrap.min.css" type="text/css" rel="stylesheet">
<center>
@include('stu.menu')
<h3>浏览学生信息</h3>
<table width="700" border="1">
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>班级</th>
<th>操作</th>
</tr>
@foreach ($list as $ob)
<tr>
<td>{{ $ob->id }}</td>
<td>{{ $ob->name }}</td>
<td>{{ $ob->sex }}</td>
<td>{{ $ob->age }}</td>
<td>{{ $ob->classid }}</td>
<td><a href="/stu/{{ $ob->id}}">查看</a></td>
</tr>
@endforeach
</table>
<br/>
{!! $list->render() !!}
</center>
十二、模板继承
1. 定义一个父模板 Blade 页面布局
<!-- Stored in resources/views/layouts/master.blade.php -->
<html>
<head>
<title>App Name - @yield('title')</title>
</head>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
2. 在视图模板中使用 Blade 页面布局
@extends('layouts.master')
@section('title', 'Page Title')
@section('sidebar')
@@parent
<p>This is appended to the master sidebar.</p>
@stop
@section('content')
<p>This is my body content.</p>
@stop
十三 数据迁移
1. 创建一个新的迁移:
命令:php artisan make:migration create_表名_table
就会在database/migrations目录下产生一个迁移类文件
迁移类包含了两个方法:up和down。
up方法用于新增表,列或者索引到数据库,[运行迁移]
down方法就是up方法的反操作,[撤销迁移]
如下代码:news新闻表
1 public function up() 2 { 3 Schema::create('news', function (Blueprint $table) { 4 $table->increments('id'); 5 $table->string('title')->index(); 6 $table->string('author',50); 7 $table->text('content'); 8 $table->integer('addtime')->unsigned(); 9 //$table->timestamps(); 10 }); 11 } 12 13 public function down() 14 { 15 Schema::drop('news'); 16 }
2.运行迁移
命令:php artisan migrate
3.回滚迁移
命令:php artisan migrate:rollback 回滚最后一个迁移
命令:php artisan migrate:reset 回滚所有迁移
十四、数据填充
1.要生成一个填充器:
可以通过Artisan命令make:seeder。所有框架生成的填充器都位于database/seeders目录:
命令:php artisan make:seeder 表名TableSeeder
如:php artisan make:seeder NewsTableSeeder
一个填充器类默认只包含一个方法:run。当Artisan命令db:seed运行时该方法被调用。
run方法可以插入任何你想插入数据库的数据。
如下代码:news表
1 public function run() 2 { 3 DB::table('news')->insert([ 4 'title' => str_random(20), 5 'author' => 内容, 6 'content' => 内容, 7 'addtime' => time()+rand(0,100000), 8 ]); 9 }