如何从public目录调用@layout及Blade指令(如@extends、@section)?
Hey there! Using Blade directives in a standalone core PHP file is totally doable—you just need to set up the Blade engine manually since it’s normally part of Laravel’s ecosystem. Here’s a step-by-step guide to get you up and running with @extends, @section, and address that @layout question too:
Prerequisite: Install Blade via Composer
First, you’ll need to pull in Laravel’s View component (which includes Blade) using Composer. Run this command in your project root:
composer require illuminate/view
Step 1: Set Up Your Directory Structure
Organize your files like this to match Blade’s expected layout:
your-project/ ├── public/ │ └── index.php (your core PHP file) ├── resources/ │ └── views/ │ ├── layouts/ │ │ └── app.blade.php (your master layout) │ └── enquiries.blade.php (your content view) ├── storage/ │ └── framework/ │ └── views/ (Blade will cache compiled views here) └── vendor/ (Composer dependencies)
Make sure the storage/framework/views directory is writable by your web server.
Step 2: Initialize the Blade Engine in Your Public PHP File
In public/index.php, add code to set up the Blade environment. This connects the view files, compiler, and renderer:
// Load Composer's autoloader require __DIR__ . '/../vendor/autoload.php'; use Illuminate\Container\Container; use Illuminate\View\Factory; use Illuminate\View\FileViewFinder; use Illuminate\View\Engines\EngineResolver; use Illuminate\View\Engines\PhpEngine; use Illuminate\View\Engines\CompilerEngine; use Illuminate\View\Compilers\BladeCompiler; use Illuminate\Filesystem\Filesystem; // Initialize core components $files = new Filesystem(); // Register view engines (PHP and Blade) $engineResolver = new EngineResolver(); $engineResolver->register('php', fn() => new PhpEngine($files)); $engineResolver->register('blade', fn() => new CompilerEngine(new BladeCompiler($files, __DIR__ . '/../storage/framework/views'))); // Tell Blade where to find your view files $viewFinder = new FileViewFinder($files, [__DIR__ . '/../resources/views']); // Create the view factory to render templates $container = new Container(); $viewFactory = new Factory($engineResolver, $viewFinder, $container); // Optional: Share global data across all views $viewFactory->share('siteName', 'My Custom Site');
Step 3: Create Your Layout and Content Views
Master Layout (resources/views/layouts/app.blade.php)
This is the template your content will extend:
<!DOCTYPE html> <html> <head> <title>@yield('title')</title> <link rel="stylesheet" href="/css/styles.css"> </head> <body> <header>My Site Header</header> <main> @yield('content') <!-- This is where your content will inject --> </main> <footer>My Site Footer</footer> </body> </html>
Content View (resources/views/enquiries.blade.php)
Use @extends and @section to hook into the layout:
@extends('layouts.app') @section('title', 'Enquiries') @section('content') <h1>Welcome to the Enquiries Page</h1> <p>Here’s where you can manage all customer enquiries.</p> @endsection
Step 4: Render the View in Your Public File
Add this line to public/index.php to output the compiled Blade view:
// Render and echo the enquiries view echo $viewFactory->make('enquiries')->render();
What About the @layout Directive?
The @layout directive is deprecated—it was used in older Laravel versions (pre-5.x) as an alternative to @extends. Modern Blade no longer supports it, so it’s strongly recommended to use @extends and @section as shown above. If you’re working with legacy code that relies on @layout, you’d need to use an older version of the Blade component (not ideal for security and compatibility).
内容的提问来源于stack exchange,提问作者Sid Menon

