又有一段时间没有学习了!迷茫,除了迷茫还是在迷茫!最近,公司接了一个laravel的项目,可惜没有phper,于是开始学习laravel,现在的情况就是还没学会走路就要开始跑了,所以遇到坑会摔得很痛!
安装出现的问题
安装步骤(5.3.*)
composer global require "laravel/installer" composer create-project --prefer-dist laravel/laravel blog "5.3.*"
出现的问题
①无法安装composer依赖
报错: php.ini 缺少mbstring
laravel/framework v5.4.9 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
解决: 放开注释extension=php_mbstring.dll
②php artisan ser能运行但页面报错
报错: The only supported ciphers are AES-128-CBC and AES-256-CBC
解决: 先运行这个命令
php artisan key:generate
再运行
php artisan config:clear
③同事安装php出现的问题:php.ini启用openssl时报错openssl.dll找不到
错误显示他的配置默认去C:/php/ext/下去找openssl.dll文件
解决: 他开的是虚拟机,修改extension_dir = “./” 路径为绝对路径④本地运行时post请求报错
报错:
TokenMismatchException in VerifyCsrfToken.php line 68: in VerifyCsrfToken.php line 68 at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49 at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64 at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37 at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59 at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104 at Pipeline->then(object(Closure)) in Router.php line 655 at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 629 at Router->dispatchToRoute(object(Request)) in Router.php line 607 at Router->dispatch(object(Request)) in Kernel.php line 268 at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53 at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EnableCrossRequestMiddleware.php line 19 at EnableCrossRequestMiddleware->handle(object(Request), object(Closure)) in Pipeline.php line 137 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46 at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137 at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104 at Pipeline->then(object(Closure)) in Kernel.php line 150 at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117 at Kernel->handle(object(Request)) in index.php line 54 at require_once('E:\phpStudy\WWW\mylaravel\blog\public\index.php') in server.php line 21
原因:laravel为了防止跨站脚本攻击(CSRF),会自动为每个活跃用户的会话生成一个 CSRF「令牌」。该令牌用于验证经过身份验证的用户是否是向应用程序发出请求的用户。
解决:在app/Http/Middleware/VerifyCsrfToken中放行需要访问的地址。如protected $except = [ // /*, /wechat, ];
⑤遇到跨域问题(laravel跨域))
运行命令
php artisan make:middleware EnableCrossRequestMiddleware
自动在app/Http/Middleware下生成一个EnableCrossRequestMiddleware.php
<?php namespace App\Http\Middleware; use Closure; class EnableCrossRequestMiddleware { /** * Handle an incoming request * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { return $next($request); }
}
增加中间件内容
<?php
namespace App\Http\Middleware;
use Closure;
class EnableCrossRequestMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$origin = $request->server('HTTP_ORIGIN') ? $request->server ('HTTP_ORIGIN') : '';
$allow_origin = [
'http://localhost:8000', // 允许跨域的列表了,可自行修改
];
if (in_array($origin, $allow_origin)) {
$response->header('Access-Control-Allow-Origin', $origin);
$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN');
$response->header('Access-Control-Expose-Headers', 'Authorization, authenticated');
$response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
$response->header('Access-Control-Allow-Credentials', 'true');
}
return $response;
}
}
在内核文件注册该中间件(在 App\Http\Kernel 类的 $middleware 属性添加,这里注册的中间件属于全局中间件)
protected $middleware = [
// more
\App\Http\Middleware\EnableCrossRequestMiddleware::class,
];
⑥composer安装报错
报错:win7下使用命令行安装的composer使用时莫名其妙报错
[JsonSchema\Exception\ResourceNotFoundException] file_get_contents(file://): failed to open stream: No such file or directory
解决:下载Composer-Setup.exe,重新安装composer