如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
laravel中文网 https://laravel-china.org/ 支持: 队列 搜索 数据库迁移 定时脚本 数据填充包 https://github.com/fzaninotto/Faker laravel思想 服务容器 服务提供者 安装环境要求 PHP>=5.6.4 扩展 openssl php extension pdo PHP extension mbstring PHP extension tokenizer php extension xml PHP extension 查看PHP版本 php -v 查看扩展 php -m 查看mysql是否开启 mysql -h127.0.0.1 -p3306 -uroot -proot PHPstorm快捷键 定位 查看代码中某个方法是如何定义的 common+鼠标左键 回转到刚才定位 common+option+键盘左键 快速文件定位 common+shift+o 输入框中输入路径 复制当行粘贴到下一行 common+d sequel pro mac数据库管理工具 composer安装laravel composer create-project laravel/laravel mylaravel "5.4.*" laravel 的启动 1. web服务器配置 https://laravel-china.org/docs/laravel/5.4/installation/1216 2. php artisan serve //http://127.0.0.1:8000> php artisan help serve // 查看帮助 php 文件夹 app 具体的逻辑 mc config 配置 database 数据库文件 public 静态文件 resources 模板文件 v routes 路由 storage 日志/缓存 (要提供读写权限) test 测试 vendor 第三方包 .env 修改数据库 判断数据库是否链接 php artisan migrate:install (数据迁移时可使用的) 路由 1. HTTP方法 Route::get('/', function () { return view('welcome'); }); Route::get('/','[控制器]@[方法]'); Route::get('/posts','\App\Http\Controller\PostController@index'); 支持http1.1 get 获取 post 创建 put 更新 patch 批量更新 delete 删除 options 查询该资源支持的方法 ------------ any 只要url符合,什么请求方式无所谓 Route::any('/posts','\App\Http\Controller\PostController@index'); match 支持某个路由 Route::match(['get','post'],'/posts','\App\Http\Controller\PostController@index'); //表单只支持get post <form action="/posts" method="POST"></form> 如果想使用put提交 Route::put('/posts','\App\Http\Controller\PostController@index'); <form action="/posts" method="POST"> <input type="hidden" name="_method" value="PUT" > 也可以使用==> {{method_field("PUT")}} </form> 2.路由参数的绑定 Route::get('/posts/{id}','\App\Http\Controller\PostController@index'); function index($id) { $id == 10; } 3.路由分组 Route::group(['prefix'=>'posts'],function(){ Route::get('/','\App\Http\Controller\PostController@index'); Route::put('/{id}','\App\Http\Controller\PostController@index'); Route::get('/add','\App\Http\Controller\PostController@index'); }) 4.绑定模型 Route::get('/post/{post}','\App\Http\Controller\PostController@index'); function show(\App\Post $post){ //传递进来的是模型 //post=>表:posts =>主键: id (laravel默认的) } //文章列表 Route::get('/posts','\App\Http\Controllers\PostController@index'); //文章详情 Route::get('/posts/{post}','\App\Http\Controllers\PostController@show'); //创建文章 Route::get('/posts/create','\App\Http\Controllers\PostController@create'); Route::post('/posts','\App\Http\Controllers\PostController@store'); //编辑文章 Route::get('/posts/{post}/edit','\App\Http\Controllers\PostController@edit'); Route::put('/posts/{post}','\App\Http\Controllers\PostController@update'); //删除 Route::get('/posts/delete','\App\Http\Controllers\PostController@delete'); 创建controller php artisan make:controller PostController // \app\Http\Controllers\PostController.php 创建路由 模板渲染 return view("post/index"); blade模板 {{}} @if @foreach 继承模型 extends/ section/ yield/ content 引入视图 include ----------------------- return view("post/index",["title"=>'this is title','isShow'=>true]); 也可以写成:return view("post/index",compact('list')); {{$title}} @if($isShow==true) ......... @endif @foreach($list as $k=>$vo) ......... @endforeach --------- 模板: 新建文件\resources\views\layout\main.blade.php @yield("content") //非共用部分 继承: @extends("layout.main") @secton("content") <div>....</div> @endsecton ----------------------- 使用migrate创建数据表 表名posts(推荐后面加上s) 外键user_id(推荐) 时间created_at/updated_at/deleted_at(推荐) 2018_12_14_020115_create_posts_table.php 1. php artisan make:migration create_posts_table 2. 在生成的文件中写入代码 public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments("id"); $table->string('title',100)->default(""); $table->text("content"); $table->integer("user_id")->default(0); $table->timestamps(); }); } 3.php artisan migrate 4.按照时间依次未执行的脚本 5.完成 id/title/content/user_id/create_at/updated_at ORM模型的创建 php artisan make:model Post (驼峰形式) 生成\app\Post.php 默认对应的表是posts class Post extends Model { protected $table = "zzlll";//定义表 } tinker测试 php artisan tinker //打开tinker 增加操作 >>> $post = new \App\Post();//new => App\Post {#701} >>> $post->title = "post1";//设置标题 => "post1" >>> $post->content = 'xxxxxxxxxxx';//设置内容 => "xxxxxxxxxxx" >>> $post->save();//保存 => true 查询 >>> \App\Post::find(2); => null >>> \App\Post::find(1);//find只能用在主键上 => App\Post {#715 id: 1, title: "post1", content: "xxxxxxxxxxx", user_id: 0, created_at: "2018-12-14 02:27:54", updated_at: "2018-12-14 02:27:54", } >>> >>> \App\Post::where("title",'pos1')->get();//按条件查找 修改 >>> $post = \App\Post::find(1); => App\Post {#715 id: 1, title: "post1", content: "xxxxxxxxxxx", user_id: 0, created_at: "2018-12-14 02:27:54", updated_at: "2018-12-14 02:27:54", } >>> $post->title = "p2" => "p2" >>> $post->save(); => true 删除 >>> $post = \App\Post::find(2); => App\Post {#716 id: 2, title: "222", content: "", user_id: 0, created_at: null, updated_at: null, } >>> $post->delete(); => true 使用model 模型查找 use \App\Post; $posts = Post::orderBy('creates_at','desc'); return view("post/index",compact('posts')); @foreach($posts as $k=>$post) {{$post->title}}//传递过来的是对象不是数组 {{$post->created_at->toFormattedDateString()}}//时间 @endforeach 快速数据填充(使用faker填充) 打开databaes/factories/ModelFactory.php 创建工厂 $factory->define(App\Post::class, function (Faker\Generator $faker){ return [ 'title'=>$faker->sentence(6), 'content'=>$faker->paragraph(5), ]; }); 执行tinker $ php artisan tinker >>> factory(App\Post::class,10)->make();//打印到页面上 >>> factory(App\Post::class,10)->create();//插入到数据库 分页 $posts = Post::orderBy('creates_at','desc')->paginate(6); {{$posts->links()}} 字符的截断 {{str_limit($post->content,100,'...')}} 模型绑定 (路由传递模型) 详情页面 <a href="/posts/{{$post->id}}" >//点击进入 Route::get('/post/{post}','\App\Http\Controller\PostController@show'); public function show(Post $post){ return view("post/show",compact('post')); } 设置时区:config\app.php 'timezone' => 'Asia/Shanghai', dd(request()); CSRF保护 提交表单时不成功 <input type="hidden" name="_token" value="{{csrf_token()}}"> ==> {{csrf_field()}} 保存到数据库 $data = ['title'=>request('title'),'content'=>request('content')]; 或者 post.php(model) protected $guarded = [];//不可注入的字段 ===>$data =Post::create(request(['title','content'])) 重定义 return redirect('/posts'); 验证器 http://www.wangmingchang.com/3454.html 14 错误提示的本地化 1.放入语言包\resources\lang\zh 2.选择语言包config/app.php 'locale' => 'cn', 表单提交三步骤 1.验证 2.逻辑 3.渲染 HTML标签转化{!! HTML内容 !!} 图片上传 <meta name="csrf-token" content="{{csrf_token()}}"> js中token 'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content') 上传图片路径\storage\app\public 实际能访问的只有public ,需要如下修改 https://laravel-china.org/docs/laravel/5.4/filesystem/1251 1.config/filesystems.php 'default' => env('FILESYSTEM_DRIVER', 'public'), 2. php artisan storage:link public function upimg(REquest $request) { $path = $request = file('wangEditorFile')->storePublicly(md5(time())); return asset('storage/'.path);//到storage查找文件 } 编辑 $post->post = request("title"); $post->content = request("content"); $post->save(); 删除 $post->delete(); 用户权限认证