Home > Article > Backend Development > How to use Blade with CakePHP?
CakePHP is a popular PHP MVC framework, and Blade is one of the very popular template engines in the Laravel framework. Although CakePHP comes with a powerful template engine, sometimes we may want to use other template engines to replace the default one.
In this article, I will introduce how to use the Blade template engine in CakePHP 3, hoping to help some developers who want to try Blade.
First, we need to install Blade, which can be done through Composer. Add dependencies to the composer.json file in the root directory of the project:
{ "require": { "illuminate/view": "5.8.*" } }
Then run the composer update
command in the terminal to install the dependencies.
Next, we need to configure CakePHP to use the Blade template engine. First, add the following code to the config/app.php file:
'View' => [ 'className' => 'CakeViewView', 'viewPath' => APP . 'Template/', 'layoutPath' => APP . 'Template/Layout/', 'templatePath' => APP . 'Template/', 'cachePath' => CACHE . 'views/', 'helpers' => [ 'Html', 'Form', 'Url' ], 'useRenderCache' => false, 'engine' => [ 'Blade' => [ 'className' => 'CakeBladeBladeEngine', 'options' => [ 'cache_path' => TMP . 'blade_cache', 'view_path' => APP . 'Template/', 'auto_reload' => true ] ] ] ]
In this configuration array, we specify the view configuration of CakePHP and add a template engine named "Blade". In Blade's options, we specify the cache path, view path and whether to automatically reload the template.
Next, we need to add a file to define the Blade engine in src/View/BladeEngine.php.
<?php namespace CakeBlade; use CakeViewEngineEngine; use IlluminateViewCompilersBladeCompiler; use IlluminateViewEnginesCompilerEngine; use IlluminateViewFactory; use IlluminateViewFileViewFinder; class BladeEngine extends Engine { public $Factory; public function __construct($view = null, $layout = null) { parent::__construct($view, $layout); $config = CakeCoreConfigure::read('App'); $viewPath = $config['Template']['templatePath']; $cachePath = $config['engine']['Blade']['options']['cache_path']; $this->Factory = new Factory(new FileViewFinder([$viewPath]), new CompilerEngine(new BladeCompiler(new Filesystem, $cachePath))); } public function render($template, $layout = null) { return $this->Factory->make($template, compact('data'))->render(); } }
In this class, we define a BladeEngine class, which inherits from the Engine class in CakePHP. In the constructor, we read the view path using CakePHP's configuration and pass it to Blade's constructor so that Blade can find the template file. Additionally, we added cache paths to improve performance. In the render function, we use Blade's Factory class to render the template.
Now that we have completed configuring and defining the Blade engine, we can start writing the template file. In CakePHP, we can create template files in the src/Template/ directory.
For example, we can create a simple Blade template in src/Template/Pages/home.blade.php:
@extends('Layout.default') @section('content') <div class="jumbotron"> <h1>Welcome to CakeBlade</h1> <p>CakePHP 3 + Blade Template Engine.</p> <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p> </div> @endsection
In this template, we use @extends to specify the Layout. The content between @section and @endsection will be inserted into the @yield('content') directive of the layout.
Now, we can render the template in the controller by calling the Blade engine. For example, add the following code in PagesController:
public function home() { $this->getViewBuilder()->setClassName('CakeBlade.Blade'); $this->set(compact('data')); $this->render('home'); }
Before rendering the template, we need to specify the view class used. We then pass the data to the view and specify the template file name to load.
Now we can visit the page in the browser to see if Blade is working properly. Enter the file name in the address bar, such as http://localhost/cake_blade/pages/home, and you should see the Blade template you just wrote, which is the same as what we defined in the template file.
Summary
In this article, we introduced how to use the Blade template engine in CakePHP 3 to replace the default template engine. In this way, we can develop web applications using the powerful syntax and features provided by Blade. If you are looking for a feature-rich template engine, Blade is a good choice.
The above is the detailed content of How to use Blade with CakePHP?. For more information, please follow other related articles on the PHP Chinese website!