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

php笔记

1、array_merge_recursive  当有重复的键名时,值不会被覆盖,而是将多个相同键名的值递归组成一个数组。

2、cos函数和javascript的cos不同,php的cos参数为弧度,js的cos为角度。

3、php递归使用局部变量传参地址&。

4、stripslashes取消转义。

5、json_encode($data,JSON_UNESCAPED_UNICODE)//中文不编译成Unicode编码。

PHP中钩子函数的实现与认识

假如有这么一段程序:

function fun(){
    fun1();
    fun2();    
}

首先程序执行完fun1()之后执行fun2()然后fun()结束。
但是,假如我们想对函数做一些变化。比如说,fun是一个解析函数,我们希望后期可以提供丰富的解析函数,而究竟用哪个函数解析,我们希望在配置文件中配置。这个时候就可以发挥钩子的力量了。
我们可以在function fun(){}中加入一个挂钩点H,然后再执行H这个函数之前,将钩子函数配置好,我么就可以根据需要来解析了。
例如:

$h=config_item("parser_fun") ;//从配置文件中获得相应的配置信息
function fun($data){
global $h;
return  $h();
}

除此意外,PHP还可以根据字符串提供自己的类,然后调用类的一个方法,传递某些参数,这些就为PHP程序的编写,以及后期的维护扩展奠定了,相当坚实的基础。
类的实现方法,大致如下:

$c=get_class_name(); //获得类的名字
$m=get_method_name(); //获得方法的名字
$k=$c->$m(); //执行类的某一个方法

原文链接:PHP中钩子函数&实现

PHP order by多条件排序

关于order by排序:
单条件排序
order by id(按照id排序默认从小到大)
order by id desc(按照id排序从大到小)
多条件排序
order by date,id(先按照date从小到大再按照id从小到大)
order by date,id desc(先按照date从大到小再按照id从大到小)
order by date desc,id(先按照date从大到小再按照id从小到大)