资讯专栏INFORMATION COLUMN

laravel使用中遇到的问题

maybe_009 / 3164人阅读

又有一段时间没有学习了!迷茫,除了迷茫还是在迷茫!最近,公司接了一个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->IlluminatePipeline{closure}(object(Request)) in Pipeline.php         line 33
  at Pipeline->IlluminateRouting{closure}(object(Request)) in       ShareErrorsFromSession.php line 49
  at ShareErrorsFromSession->handle(object(Request), object(Closure)) in      Pipeline.php line 137
  at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php         line 33
  at Pipeline->IlluminateRouting{closure}(object(Request)) in StartSession.php      line 64
  at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line      137
  at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php         line 33
  at Pipeline->IlluminateRouting{closure}(object(Request)) in       AddQueuedCookiesToResponse.php line 37
  at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in      Pipeline.php line 137
  at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php         line 33
  at Pipeline->IlluminateRouting{closure}(object(Request)) in       EncryptCookies.php line 59
  at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php         line 137
  at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php         line 33
  at Pipeline->IlluminateRouting{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->IlluminateFoundationHttp{closure}(object(Request)) in         Pipeline.php line 53
  at Pipeline->IlluminateRouting{closure}(object(Request)) in       EnableCrossRequestMiddleware.php line 19
  at EnableCrossRequestMiddleware->handle(object(Request), object(Closure)) in        Pipeline.php line 137
  at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php         line 33
  at Pipeline->IlluminateRouting{closure}(object(Request)) in       CheckForMaintenanceMode.php line 46
  at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in         Pipeline.php line 137
  at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php         line 33
  at Pipeline->IlluminateRouting{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:phpStudyWWWmylaravellogpublicindex.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

  
   

            }
                
   

增加中间件内容

        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;
            }
        }

    在内核文件注册该中间件(在 AppHttpKernel 类的 $middleware 属性添加,这里注册的中间件属于全局中间件)

            protected $middleware = [
                // more
                AppHttpMiddlewareEnableCrossRequestMiddleware::class,
            ];

⑥composer安装报错

报错:win7下使用命令行安装的composer使用时莫名其妙报错

[JsonSchemaExceptionResourceNotFoundException] file_get_contents(file://): failed to open stream: No such file or directory

解决:下载Composer-Setup.exe,重新安装composer

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28917.html

相关文章

  • laravel安装jenssegers/laravel-mongodb拓展,以及遇到

    摘要:再次打开包内源码,发现了如下的细节敲黑板注意看路径,此时是包内知道原理之后,于是在配置文件中做了如下修改至此,,收工 项目中使用了mongoDB,所以就到最大的同性交友网站找到了星星最多的jenssegers/laravel-mongodb 包安装 1、根据README,找到本地laravel对应的包版本showImg(https://segmentfault.com/img/bVbr...

    icattlecoder 评论0 收藏0
  • laravel安装jenssegers/laravel-mongodb拓展,以及遇到

    摘要:再次打开包内源码,发现了如下的细节敲黑板注意看路径,此时是包内知道原理之后,于是在配置文件中做了如下修改至此,,收工 项目中使用了mongoDB,所以就到最大的同性交友网站找到了星星最多的jenssegers/laravel-mongodb 包安装 1、根据README,找到本地laravel对应的包版本showImg(https://segmentfault.com/img/bVbr...

    fizz 评论0 收藏0
  • PHPer、Laravel 面试可能会遇到问题及答案

    摘要:如何实现持久化持久化,将在内存中的的状态保存到硬盘中,相当于备份数据库状态。相当于备份数据库接收到的命令,所有被写入的命令都是以的协议格式来保存的。 最近社区里面有一篇文章引起了最多程序猿的关注,Laravel、PHPer 面试可能会遇到的问题,看评论区不少小伙伴们被难倒,对于一些问题同样难倒了我(其实有很多啦),趁着周末有空,又总结梳理了一遍,顺便来答一波题。由于个人技术水平有限,答...

    fanux 评论0 收藏0

发表评论

0条评论

maybe_009

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<