laravel使用中遇到的問題

胡琦發表於2019-02-16

又有一段時間沒有學習了!迷茫,除了迷茫還是在迷茫!最近,公司接了一個laravel的專案,可惜沒有phper,於是開始學習laravel,現在的情況就是還沒學會走路就要開始跑了,所以遇到坑會摔得很痛!

安裝出現的問題

  1. 安裝步驟(5.3.*)

                    
     composer global require "laravel/installer" 
     composer create-project --prefer-dist laravel/laravel blog "5.3.*"
  2. 出現的問題

    ①無法安裝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

        
         <?php
      
         namespace AppHttpMiddleware;
      
         use Closure;
      
         class EnableCrossRequestMiddleware
         {
             /**
              * Handle an incoming request
              * @param  IlluminateHttpRequest  $request
              * @param  Closure  $next
              * @return mixed
                  */
                 public function handle($request, Closure $next)
                 {
                     return $next($request);
                 }
            }
                
   

增加中介軟體內容

        <?php
        namespace AppHttpMiddleware;
        use Closure;
        class EnableCrossRequestMiddleware
        {
            /**
             * Handle an incoming request.
             *
             * @param  IlluminateHttpRequest $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;
            }
        }

    在核心檔案註冊該中介軟體(在 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

相關文章