Php Laravel 笔记

Laravel 可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。

Blade 模板相关

php 变量输出为 html 代码

{!! $content !!}

获取当前路由路径

{{ Request::path() }}
{{ Route::currentRouteName() }}

获取当前路由参数

{{ app('request')->input('param') }}
{{ Request::query('param') }}         // laravel 5.6
{{ request()->param }}                // laravel 5.8

分页传入路由参数

{{ $data->appends(['param' => request()->param])->links('path_to_pagination_view') }}

将视图转化为字符串

$view = view('emails.index')->with(['lang' => $lang, 'name' => $name]);
$viewStr = response($view)->getContent();

Form 表单进行 PUT 请求

<form method="post" action="/tc-strategy/{{ $unionId }}">
    <input name="_method" type="hidden" value="PUT"/>
    {{ csrf_field() }}
</form>

Eloquent 模型相关

ORM 查询指定列记录

  • 按 id 取单条记录

      $data = Model::find($id, ['column1', 'column2']);
    
  • 取集合首条记录

      $data = Model::first(['column1', 'column2']);
    
  • 取集合全部记录

      $data = Model::all(['column1', 'column2']);
    
  • 取集合多条记录

      $data = ModelA::where(...)->get(['column1', 'column2']); 
    

Redis 相关

使用 Redis 发布与订阅消息队列

  1. 安装 predis 客户端

     composer require predis/predis
    
  2. 发布者 Command

     try {
         Redis::publish($QUEUE_NAME, $message);
     } catch (Exception $e) {
         this->info('$e');
     }
    
  3. 订阅者 Command

     Redis::subscribe([$QUEUE_NAME], function($message) {
         this->info($message);
     });
    

使用 Redis 异步任务队列

  1. 安装 predis 客户端

     composer require predis/predis
    
  2. 修改环境变量 QUEUE_DRIVER

     QUEUE_CONNECTION=redis
    
  3. 创建失败任务迁移文件

     php artisan queue:failed-table
    
  4. 生成失败任务表

     php artisan migrate
    
  5. 生成任务类

     php artisan make:job SendEmailJob
    
  6. 编写任务执行逻辑

     namespace App\Jobs;
    
     use Illuminate\Bus\Queueable;
     use Illuminate\Queue\SerializesModels;
     use Illuminate\Queue\InteractsWithQueue;
     use Illuminate\Contracts\Queue\ShouldQueue;
     use Illuminate\Foundation\Bus\Dispatchable;
    
     class SendEmailJob implements ShouldQueue {
         use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
         protected $injectionObject;
    
         public function __construct(Object $object) {
             $this->injectionObject = $object;
         }
    
         public function handle() {
             // TODO: 任务执行方法体
         }
     }
    
  7. 发送任务到任务队列

     public function work() {
         $job = new SendEmailJob($obj);
         dispatch(job);                  // 分发任务
     }
    
  8. 监听队列

     php artisan queue:work --daemon --quiet --queue=default --delay=3 --sleep=3 --tries=3
    
  9. 仪表盘监控队列(仅 Linux)

     composer require "laravel/horizon:~1.3"
     php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"
     php artisan horizon
    

    访问 http://localhost/horizon 即可访问仪表盘

使用 Redis 缓存

  1. 修改配置文件
// config/cache.php
'redis' => [
    'driver'     => 'redis',
    'connection' => 'default',
],
// config/database.php
'redis' => [
    'client' => 'predis',

    'default' => [
        'host'     => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port'     => env('REDIS_PORT', 6379),
        'database' => 0,
    ],
],
  1. 使用 Redis 缓存
if (Cache::has($key)) {
    $value = Cache::get($key);
    // use $value
} else {
    Cache::put($key, $value, $expire_time);
}

HTTP 相关

HTTP 客户端

  • php-curl-class

      use \Curl\Curl;
    
      $curl = new Curl();
      $curl->get('http://www.example.com/');
    
      if ($curl->error) {
          echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage;
      } else {
          echo $curl->response;
      }
    
  • GuzzleHttp\Client

      use GuzzleHttp\Client;
    
      $http = new Client();
      $url = 'http://www.baidu.com';
      $response = $http->get($url);
      $data = json_decode((string)$response->getBody(), true);
    

MVC 相关

返回默认的 error 视图

view()->replaceNamespace('errors', [
    resource_path('views/errors'),
    __DIR__.'/views',
]);
return response()->view("errors::{$status}";

返回 Json 数据

return response()->json(['code' => '1', 'msg' => 'Subscribe successfully']);

Composer 相关

Composer 换源

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

查看配置

composer config -gl

新建 laravel 项目

composer create-project laravel/laravel <projectName>

部署相关

Nginx

  1. 将网站目录指向 public 文件夹

    root "D:/WORKSPACE/PhpStorm/up_server_admin/public";

  2. 设置网站入口为 index.php

    index index.php index.html;

  3. 设置所有资源入口为 index.php

    try_files $uri $uri/ /index.php?$query_string;

正在加载今日诗词....

Access Statistics

Table of Contents