spatie / laravel-demo-mode
Requires
- php: ^8.0.2|^8.1
- illuminate/support: ^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^7.1|^8.0
- phpunit/phpunit: ^9.3
README
想象一下,你正在开发一个新应用。你的客户想要看到你所取得的进度。然而,你的网站还没有准备好投入使用。当然,你可以创建一些登录功能,只向登录用户显示网站。但是,为什么还要费心创建用户呢?有更实际的方法。
该软件包提供了一个路由中间件,用于保护受保护的路由免受好奇之人的窥探。所有访问受保护路由的用户都将被重定向到可配置的URL(例如 /under-construction
)。当用户尝试访问未知路由时也是如此。要查看路由内容,访问者必须首先访问授予访问权限的URL(例如 /demo
)。
提醒一句:不要使用此软件包来限制对敏感数据的访问或保护管理区域。对于这些情况,您应使用适当的身份验证。
Spatie是一家位于比利时的安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述在这里。
注意
如果您使用的是Laravel 8或更高版本,请使用Laravel内置的php artisan down
命令来激活演示模式。您不需要这个软件包。
支持我们
我们投入了大量资源来创建一流的开放源代码软件包。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从家乡寄给我们明信片,并说明您正在使用我们哪些软件包。您可以在我们的联系页面上找到我们的地址。我们将发布收到的所有明信片在我们的虚拟明信片墙上。
安装
您可以通过composer安装该软件包
composer require spatie/laravel-demo-mode
Spatie\DemoMode\DemoModeServiceProvider::class
服务提供器将被自动注册。
必须将\Spatie\DemoMode\DemoMode::class
中间件注册到内核中
//app/Http/Kernel.php protected $routeMiddleware = [ ... 'demoMode' => \Spatie\DemoMode\DemoMode::class, ];
将路由中间件命名为DemoMode
只是建议。您可以给它任何您想要的名称。
您必须发布配置文件
php artisan vendor:publish --provider="Spatie\DemoMode\DemoModeServiceProvider"
这是已发布配置文件demo-mode.php
的内容
return [ /* * This is the master switch to enable demo mode. */ 'enabled' => env('DEMO_MODE_ENABLED', true), /* * Visitors browsing a protected url will be redirected to this path. */ 'redirect_unauthorized_users_to_url' => '/under-construction', /* * After having gained access, visitors will be redirected to this path. */ 'redirect_authorized_users_to_url' => '/', /* * The following IP's will automatically gain access to the * app without having to visit the `demoAccess` route. */ 'authorized_ips' => [ // ], /* * When strict mode is enabled, only IP's listed in `authorized_ips` will gain access. * Visitors won't be able to gain access by visiting the `demoAccess` route anymore. */ 'strict_mode' => false, ];
如果您想使用demoAccess
路由,必须在路由文件中调用demoAccess
路由宏。
Route::demoAccess('/demo');
访问/demo
将授予访问由演示模式保护的页面的权限。当然,您可以选择任何您想要的URL。
如果您想自动授权某些IP地址,可以将它们添加到demo-mode.php
配置文件中的authorized_ips
数组中。
要禁用demoAccess
路由并仅允许访问authorized_ips
,可以在demo-mode.php
配置文件中启用strict_mode
。
用法
您可以通过在它们上使用demoMode
中间件来保护一些路由。
//only users who have previously visited "/demo" will be able to see these pages. Route::group(['middleware' => 'demoMode'], function () { Route::get('/secret-route', function() { echo 'Hi!'; }); });
除非您首先访问由 demoAccess
路由宏使用的 URL 或从授权 IP 地址访问,否则访问这些路由将导致重定向到配置文件中 redirect_unauthorized_users_to_url
-键指定的 URL。
认证用户也可以访问所有受保护的路线。
因为它使用会话来验证用户,所以 demoAccess
路由和受保护的路由都必须有 web
中间件,或者有 \Illuminate\Session\Middleware\StartSession
中间件,才能授权那些未认证或未从授权 IP 访问的用户。
更新日志
有关最近更改的更多信息,请参阅 更新日志。
测试
composer test
贡献
有关详细信息,请参阅 贡献指南。
安全
如果您发现有关安全性的错误,请发送电子邮件到 security@spatie.be 而不是使用问题跟踪器。
鸣谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件。