laravel5.7 redis socket.io

composer require predis/predis

.env

BROADCAST_DRIVER=redis

config/app.php

取消注释providers下App\Providers\BroadcastServiceProvider::class,

创建文件app/Events/ChatRoom.php

<?php

namespace App\Events;

use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ChatRoom implements ShouldBroadcast
{

    public $data;
    private $channels;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($channels, $data)
    {
        //
        $this->channels = $channels;
        $this->data = $data;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return $this->channels;
    }
}

调用

use Event, App\Events\ChatRoom;

Event::fire(new ChatRoom($channel, ‘data’));

laravel5找回密码改中文

5.7

lang/zh_cn.json

{
    "Login": "登陆",
    "E-Mail Address": "邮箱",
    "Password": "密码",
    "Remember Me": "记住我",
    "Forgot Your Password?": "忘记密码?",
    "Register": "注册",
    "Name": "姓名",
    "Confirm Password": "重复密码",
    "Reset Password": "重设密码",
    "Send Password Reset Link": "发送邮件",
    "Logout": "退出",
    "Reset Password Notification": "找回密码",
    "You are receiving this email because we received a password reset request for your account.": "您收到此电子邮件,因为我们收到了找回密码请求您的账户。",
    "If you did not request a password reset, no further action is required.": "如果没有请求找回密码,则不需要进一步的操作。",
    "Hello!": "您好!",
    "Regards": "真挚问候",
    "If you’re having trouble clicking the ": "如果您不能点击"
}

5.4

php artisan vendor:publish --tag=laravel-notifications//找回密码邮件模版
php artisan make:notification ResetPassword//找回密码邮件通知

App/Notifications/Resetpassword.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPassword extends Notification
{
    use Queueable;

    /**
     * The password reset token.
     *
     * @var string
     */
    public $token;

    /**
     * Create a notification instance.
     *
     * @param  string  $token
     * @return void
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('找回密码')
            ->line('您收到此电子邮件,因为我们收到了找回密码请求您的账户。')
            ->action('重设密码', route('password.reset', $this->token))
            ->line('如果没有请求找回密码,则不需要进一步的操作。');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

app\User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\ResetPassword as ResetPasswordNotification;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));
    }
}

views/vendor/notifications/email.blade.php

@component('mail::message')
{{-- Greeting --}}
@if (! empty($greeting))
# {{ $greeting }}
@else
@if ($level == 'error')
# 错误!
@else
# 您好!
@endif
@endif

{{-- Intro Lines --}}
@foreach ($introLines as $line)
{{ $line }}

@endforeach

{{-- Action Button --}}
@if (isset($actionText))
<?php
    switch ($level) {
        case 'success':
            $color = 'green';
            break;
        case 'error':
            $color = 'red';
            break;
        default:
            $color = 'blue';
    }
?>
@component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
@endcomponent
@endif

{{-- Outro Lines --}}
@foreach ($outroLines as $line)
{{ $line }}

@endforeach

<!-- Salutation -->
@if (! empty($salutation))
{{ $salutation }}
@else
来自,<br>{{ config('app.name') }}
@endif

<!-- Subcopy -->
@if (isset($actionText))
@component('mail::subcopy')
如果您不能点击"{{ $actionText }}"按钮,请复制下面的网址粘贴到您的浏览器:
[{{ $actionUrl }}]({{ $actionUrl }})
@endcomponent
@endif
@endcomponent

laravel常用命令

//执行迁移
php artisan migrate
//数据回滚
php artisan migrate:rollback
//webpack生产
npm run production
//发布webpack
npm run dev
//重建节点
npm rebuild node-sass
//创建新表
php artisan make:migration create_表名_table --create=表名
//填充数据
php artisan db:seed
//添加字段
php artisan make:migration add_添加信息_to_表名_table --table=表名
//创建控制器
php artisan make:controller 控制器复数Controller --resource
//创建模块
php artisan make:model 模块 -m
//创建视图
php artisan generate:view 文件夹.视图
//创建控制器模块数据库增删改查文件
php artisan generate:resource 表名
//查看路由
php artisan route
//自动加载
composer dumpautoload
//重置key
php artisan key:generate

laravel npm run dev error

错误:SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

node版本低,node5.0在文件第一行加 ”use strict”,或者更新node


brew upgrade node#更新node

curl -sL https://deb.nodesource.com/setup_6.x | bash –

apt-get install nodejs

错误:cross-env NODE_ENV=development webpack –watch –progress –hide-modules –config=node_modules/laravel-mix/setup/webpack.config.js

npm rebuild node-sass

npm run dev#successfully

laravel5、php7、mongodb学习

使用laravel-mongodb

mongodb php driver

php7需要mongodb1.1x

PHP Driver PHP 5.3 PHP 5.4 PHP 5.5 PHP 5.6 PHP 7.0 HHVM 3.9
mongodb-1.1  
mongodb-1.0    
mongo-1.6    
mongo-1.5    

下载mongodb之后使用phpize安装

phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
#php.ini加
extension =mongodb.so
php composer.phar require jenssegers/mongodb  

提示错误,php扩展mbstring错误,用phpize编译安装mbstring

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - danielstjules/stringy 1.10.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - danielstjules/stringy 1.10.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - danielstjules/stringy 1.10.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - Installation request for danielstjules/stringy == 1.10.0.0 -> satisfiable by danielstjules/stringy[1.10.0].


Installation failed, reverting ./composer.json to its original content.

Laravel错误

5.1在php7环境上运行出现的错误

Fatal error: Uncaught ReflectionException: Class log does not exist in /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php:736 Stack trace: #0 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(736): ReflectionClass->__construct(‘log’) #1 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(626): Illuminate\Container\Container->build(‘log’, Array) #2 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(674): Illuminate\Container\Container->make(‘log’, Array) #3 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(837): Illuminate\Foundation\Application->make(‘log’) #4 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(800): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter)) #5 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(769): Illuminate\Container\Container in /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 736

解决方案:

删除vendor文件夹和composer.lock

composer install

laravel 500错误是storage没有权限

解决方案:chmod -R 777 storage

cache错误

file_put_contents(/..cache/services.json): failed to open stream: Permission denied

解决方案:sudo php artisan cache:clear

php composer.phar install报错

先安装composer,然后运行php composer.phar install –no-scripts

PDO错误,已安装PDO还提示

[PDOException]
SQLSTATE[HY000] [2002] No such file or directory

解决方案:将.env下的DB_HOST=localhost改为DB_HOST=127.0.0.1

SQLSTATE[HY000] [1045]

解决方案:
php artisan cache:clear
php artisan config:clear

php artisan 没反应

composer dumpautoload

The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.

php artisan key:generate

[Dotenv\Exception\InvalidFileException]
Dotenv values containing spaces must be surrounded by quotes.

.env文件中文或者空格必须加引号

OpenSSL extension is required

打开php.ini启用php_openssl

Key path “file://storage\oauth-public.key” does not exist or is not readable

php artisan passport:install

[Composer\Downloader\TransportException] The “https://packagist.phpcomposer.com/p/provider-2017-07%24c3e8d929d5d06fa b76cef9c5b5e4305dbe89c1599b79e63eee70490a6b8df914.json” file could not be d ownloaded (HTTP/1.1 404 Not Found)

composer diagnose

[ErrorException] proc_open(): fork failed – Cannot allocate memory

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024

/sbin/mkswap /var/swap.1

/sbin/swapon /var/swap.1

LogicException Key path “file:///storage/oauth-public.key” does not exist or is not readable

chown www-data:www-data storage/oauth-*.key

chmod 600 storage/oauth-*.key

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))

AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

C:\ProgramData\MySQL\MySQL Server 8.0\my.ini里default_authentication_plugin=caching_sha2_password改default_authentication_plugin=mysql_native_password

exception:”InvalidArgumentException”
file:”C:\web\jinguan\vendor\laravel\framework\src\Illuminate\Http\JsonResponse.php”
line:75
message:”Malformed UTF-8 characters, possibly incorrectly encoded”

Event::fire错误是redis没开

Class session does not exist

chmod -R 777 bootstrap/cache

Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)

php.ini打开扩展

extension=fileinfo