文章

Lavarel教程笔记

Lavarel教程笔记

Lavarel教程

[https://www.laravist.com/series/laravel-5-basic/episodes/1]

一:安装和基本运行

安装Composer:curl -sS https://getcomposer.org/installer | php
下载composer.phar文件,将可执行文件phar放在PATH目录中
composer create-project laravel/laravel xxx
开启web服务:

  1. 使用php内置服务器:php -S localhost:8888 -t public
  2. 使用php artison serve

    二:Lavarel的工作流程

    1.路由(HTTP)
    2.

    三:Lavarel向视图传递变量(V)

    输出变量:

    #name=’xxx’;return view(‘sites.about’)->with(‘name’)
    传统方法(PHP):<?= $name ?>
    模板方法: 或者{!! !!}(不转意)
    {!! $name !!}的写法和<?= $name ?>的写法是类似的

    其他输出方式:

    • 使用with返回:return view(‘test’)->with(‘name’, $name);
    • 使用with返回数组:return view(‘test2’)->with( [‘first’=>’Hello’, ‘second’=>’World’]);
    • 使用data变量:$data = [];$data[‘first’] = ‘first’;$data[‘second’] = ‘second’;return view(‘test2’, $data);
    • 使用compact返回:return view(‘test2’, compact(‘first’, ‘second’));

      四:Blade的用法(V)

      公用模板

      引入其他文件:@yield(‘content’) @yield(‘footer’)
      被其他文件引用:@extends(‘app’) @section(‘content’) @stop @section(‘footer’) @stop

      条件判断

      传统方法:<?php if xxx ?>
      blade方法:@if($first == ‘xxx’) @else @endif

      循环判断

      模板方法:@foreach($people as $person) @foreach
      @if(count($people)> 0) @endif

      五:Lavarel的环境配置(.env文件)

      键(大写,横线隔断)=值(小写)
      config/database.php文件引用了env环境文件,将env设置为gitignore可以防止密码的泄露,做到合理的解耦

      六:Migration基础(数据库的版本控制,M)

      database/migration/users_table和 xx/xx/password_resets_table文件用来定义数据库信息,使用php artison migrate命令可以用来注册数据库
      php arrison make:migration create-articles_table –create=articles

      八:Eloquent入门

      php artison make:model article
      php artsion tincker:类似于php的交互界面
      $article= new App\Article;
      $article->title=’My First Article’;
      $article->content=’content’;
      $article->published_at=Carbon\Carbon::now();
      $article->save();
      $article->toArray();
      $first=App\Article::find(1);
      $first->title=’Update’;
      $first->save();
      $second=App\Article::where(‘content’,’=’,’content’)->get();
      $second=App\Article::where()->first();
      $article=App\Article::create([‘title’=>’Second Title’, ‘content’=>’Second Content’, ‘published_at’=>Carbon\Carbon::now()]);
      $article->update([‘title’=>’Change Title’]);

本文由作者按照 CC BY 4.0 进行授权

热门标签