Skip to content

Zeroplex 生活隨筆

軟體開發和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

標籤: Laravel

Customize HTTP header in Unit Test on Lumen

Posted on 2022 年 2 月 23 日2022 年 2 月 23 日 By 日落 在〈Customize HTTP header in Unit Test on Lumen〉中有 1 則留言

按照 Lumen 文件,在 unit test 中建立 HTTP request 要呼叫 $this->call(),但沒辦法自訂 header 內容。

細看實作 Laravel\Lumen\Testing\Concerns\MakesHttpRequests::call():

/**
 * Call the given URI and return the Response.
 *
 * @param  string  $method
 * @param  string  $uri
 * @param  array  $parameters
 * @param  array  $cookies
 * @param  array  $files
 * @param  array  $server
 * @param  string  $content
 * @return \Illuminate\Http\Response
 */
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
    $this->currentUri = $this->prepareUrlForRequest($uri);

    $symfonyRequest = SymfonyRequest::create(
        $this->currentUri, $method, $parameters,
        $cookies, $files, $server, $content
    );

    $this->app['request'] = LumenRequest::createFromBase($symfonyRequest);

    return $this->response = TestResponse::fromBaseResponse(
        $this->app->prepareResponse($this->app->handle($this->app['request']))
    );
}

從 function parameter 和 return,大概就知道使用這個方法沒辦法塞 header 值。

但是若透過 SymfonyRequest 的話,則可以設定 header 內容:

$request = SymfonyRequest::create();
$request->headers->set('acccess_token', 'ooxx');

因此,若需要自訂 header,則必須 override 原本的 call() 方法。以下是自己改寫的 trait:

use Illuminate\Testing\TestResponse;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

trait ApiTokenRequestTrait
{
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null, $headers = [])
    {
        $symfonyRequest = SymfonyRequest::create(
            $uri,
            $method,
            $parameters,
            $cookies,
            $files,
            $server,
            $content
        );

        foreach ($headers as $key => $value) {
            $symfonyRequest->headers->set($key, $value);
        }

        $this->app['request'] = \Laravel\Lumen\Http\Request::createFromBase($symfonyRequest);

        return TestResponse::fromBaseResponse(
            $this->app->prepareResponse($this->app->handle($this->app['request']))
        );
    }
}
Tags:Laravel, Lumen, PHP

在 Laravel 建立並自動更新 sitemap.xml

Posted on 2022 年 2 月 17 日2022 年 2 月 17 日 By 日落 在〈在 Laravel 建立並自動更新 sitemap.xml〉中有 1 則留言

sitemap.xml 應該算是 SEO 中相當重要的一個檔案,告訴搜尋引擎到底網站中有哪些資源是公開瀏覽的,可惜 Laravel framework 並沒有支援這項功能,必須透過其他工具達成。

以下介紹二個目前看到比較欣賞的解決方法。

More “在 Laravel 建立並自動更新 sitemap.xml” »

Tags:Laravel, PHP, 網路架站

Laravel 9 released

Posted on 2022 年 2 月 9 日2022 年 2 月 9 日 By 日落 在〈Laravel 9 released〉中尚無留言

Laravel 9 released,除了長期支援到 2024-02-08 以外,還有一些異動,像是 PHP 僅支援 8.x 或以上。

詳細內容請參考官方說明:https://laravel-news.com/laravel-9-released

Tags:Laravel, PHP

Laravel v7.0 新功能

Posted on 2020 年 4 月 21 日2021 年 3 月 12 日 By 日落 在〈Laravel v7.0 新功能〉中尚無留言

從官方網站整理下來:

Airlock

使用 Airlock 可以更輕鬆的支援 single page application (SPA)、token-based API 等等,同時也可以針對 token 設定可存取犯元 (scope)。 (感覺上類似 OAuth)

Custom Eloquent Casts

以往使用者從 Eloquent ORM 拉出資料以後,使用者可以自訂 cast type,也就是說可用者可以自動 cast attribute 成特定格式。範例可以參考 Laravel 的 Pull Request。

Blade

新增不少功能來 render HTML。詳細說明請參考 Larvel Docs。

HTTP Client

為了史 API 更靈活,Laravel 使用 Guzzle HTTP client 來與其他 API 溝通。

Routing Cache

透過新的方式來做 route 的 cache,即使大型網站 (800 routes) ,route matching 的速度也可以比以往快上 20 倍。

Tags:Laravel, PHP

Laravel UrlGenerator 判斷 HTTP or HTTPS 的因素

Posted on 2019 年 10 月 7 日2021 年 3 月 12 日 By 日落 在〈Laravel UrlGenerator 判斷 HTTP or HTTPS 的因素〉中尚無留言

第一個要求 Laravel 要使用 HTTP or HTTPS (以下簡稱 protocol … 因為字太多了 .._Drz) 的方法,其實就是在「.env」做設定,例如:

APP_URL=http://zeroplex.tw

再來網路上應該會找到,要開發者在 AppServiceProvider 中加入設定:

URL::forceScheme('https');

這邊的 URL facade,可以從 config/app.php 中追蹤到,是 IlluminateRoutingUrlGenerator::class 的 alias,設定了 forceSheme() 以後,未來透過 UrlGenerator 建立的 link 都會是設定好的 protocol。

BUT …. 就是這個 BUT,若是遇到另一個其他的 method 來產生 URL (例如 IlluminateRoutingRedirector::route() …),這樣就可能吃到開發者設定的 protocol ,則由 Laravel 會自動抓進來的 request 來判斷該用什麼 protocol,若 application 剛好是靠 Nginx 在 handle HTTP SSL 的話,request 到 Laravel 這端時 HTTPS 就已經轉成 HTTP,導致 Laravel 以為 application 沒有上 HTTPS。

若要讓 reverse proxy 後面的 Laravel application 都固定用 HTTPS 的話,可以再到 app/Http/Middleware/TrustedProxy.php 去修改設定,讓 Laravel 識別 proxy 做出對應的動作 (給正確的 protocol)。

Ref: Laravel 5 force HTTPS issue with login routing to HTTP

Tags:AWS, Laravel, Nginx, PHP

文章分頁

上一頁 1 ... 3 4 5 下一頁

其他

關於我  (About me)

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Raspberry Pi Ubuntu Unix Vim Web Windows XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.