laravel使用中遇到的問題

又有一段時間沒有學習了!迷茫,除了迷茫仍是在迷茫!最近,公司接了一個laravel的項目,惋惜沒有phper,因而開始學習laravel,如今的狀況就是還沒學會走路就要開始跑了,因此遇到坑會摔得很痛!

安裝出現的問題

  1. 安裝步驟(5.3.*)php

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

    ①沒法安裝composer依賴

    報錯: php.ini 缺乏mbstringgit

    laravel/framework v5.4.9 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.

    解決: 放開註釋extension=php_mbstring.dllgithub

    ②php artisan ser能運行但頁面報錯

    報錯: The only supported ciphers are AES-128-CBC and AES-256-CBC跨域

    解決: 先運行這個命令app

    php artisan key:generate

    再運行composer

    php artisan config:clear

    ③同事安裝php出現的問題:php.ini啓用openssl時報錯openssl.dll找不到

    錯誤顯示他的配置默認去C:/php/ext/下去找openssl.dll文件
    解決: 他開的是虛擬機,修改extension_dir = "./" 路徑爲絕對路徑ide

    ④本地運行時post請求報錯

    報錯: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

相關文章
相關標籤/搜索