該軟體包為您的網站添加了一條簡單的、可自訂的 cookie 同意訊息。當網站載入時,會出現橫幅並讓使用者同意 cookie。一旦獲得同意,橫幅就會隱藏並保持隱藏狀態。
這個包沒有什麼:
對於 Laravel 中更進階的 cookie 同意選項,請考慮這些替代方案。
我們投入了大量資源來創建一流的開源套件。您可以透過購買我們的一款付費產品來支持我們。
我們非常感謝您從家鄉寄給我們一張明信片,並註明您正在使用我們的哪種套餐。您可以在我們的聯絡頁面上找到我們的地址。我們在虛擬明信片牆上發布所有收到的明信片。
您可以透過 Composer 安裝該軟體包:
composer require spatie/laravel-cookie-consent
該包將自動註冊。
您可以選擇發布設定檔:
php artisan vendor:publish --provider= " SpatieCookieConsentCookieConsentServiceProvider " --tag= " cookie-consent-config "
這是已發布的設定檔的內容:
return [
/*
* Use this setting to enable the cookie consent dialog.
*/
' enabled ' => env ( ' COOKIE_CONSENT_ENABLED ' , true ),
/*
* The name of the cookie in which we store if the user
* has agreed to accept the conditions.
*/
' cookie_name ' => ' laravel_cookie_consent ' ,
/*
* Set the cookie duration in days. Default is 365 * 20.
*/
' cookie_lifetime ' => 365 * 20 ,
];
cookie 網域由 config/session.php 中的「domain」鍵設置,請確保在 .env 中為 SESSION_DOMAIN 新增一個值。如果您使用的網域在 url 中包含連接埠(例如「localhost:3000」),則在您這樣做之前此軟體包將無法運作。
要顯示該對話框,您只需將此視圖包含在範本中即可:
//in your blade template
@include ( ' cookie-consent::index ' )
這將呈現以下對話框,該對話框在設計樣式時看起來非常像這個。
該套件提供的預設樣式使用 TailwindCSS v2 在頁面底部提供浮動橫幅。
當使用者按一下「允許 cookie」時,將設定laravel_cookie_consent
cookie,並且對話方塊將從 DOM 中刪除。在下一個請求中,Laravel 會注意到laravel_cookie_consent
已設置,並且不會再次顯示對話框
如果您想要修改對話方塊中顯示的文本,可以使用以下命令發布 lang 檔案:
php artisan vendor:publish --provider= " SpatieCookieConsentCookieConsentServiceProvider " --tag= " cookie-consent-translations "
這會將此文件發佈到resources/lang/vendor/cookie-consent/en/texts.php
。
return [
' message ' => ' Please be informed that this site uses cookies. ' ,
' agree ' => ' Allow cookies ' ,
];
例如,如果您想將這些值翻譯為法語,只需將該檔案複製到resources/lang/vendor/cookie-consent/fr/texts.php
並填寫法語翻譯即可。
如果您需要完全控制對話框的內容。您可以發佈包的視圖:
php artisan vendor:publish --provider= " SpatieCookieConsentCookieConsentServiceProvider " --tag= " cookie-consent-views "
這會將index
和dialogContents
視圖檔案複製到resources/views/vendor/cookie-consent
。您可能只想修改dialogContents
視圖。如果您需要修改此套件的 JavaScript 程式碼,您可以在index
視圖檔案中進行操作。
您可以選擇將SpatieCookieConsentCookieConsentMiddleware
新增至核心中,而不是在視圖中包含cookie-consent::index
// app/Http/Kernel.php
class Kernel extends HttpKernel
{
protected $ middleware = [
// ...
Spatie CookieConsent CookieConsentMiddleware::class,
];
// ...
}
這會自動將cookie-consent::index
加入到回應內容的結束正文標記之前。
我們不是律師,無法提供法律建議。諮詢法律專業人士哪些規則適用於您的專案。
請參閱變更日誌以了解最近變更的更多資訊。
composer test
詳細資訊請參閱貢獻。
如果您發現任何與安全相關的問題,請發送電子郵件至 [email protected],而不是使用問題追蹤器。
麻省理工學院許可證 (MIT)。請參閱許可證文件以獲取更多資訊。