我在Bootstrap/app.php中注册了中间件(laravel 11中的kernel.php的替代方案)这样: `
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use App\Http\Middleware\ContentSecurityPolicy;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__ . '/../routes/web.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->prepend(ContentSecurityPolicy::class);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
但如果在内联脚本中使用$ nonce:
<script nonce="{{$nonce}}"></script>
我得到此错误:“未定义的变量$ nonce”。 我该如何解决?
view()->share("nonce",$nonce);
前
$response = $next($request);
所有的一切都会如预期的。