How to use Blade with CakePHP?

WBOY
Release: 2023-06-04 10:04:01
Original
1412 people have browsed it

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.

  1. Installing 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.*"
    }
}
Copy after login

Then run the composer update command in the terminal to install the dependencies.

  1. Configuring CakePHP

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
            ]
        ]
    ]
]
Copy after login

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();
    }
}
Copy after login

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.

  1. Create the template file

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
Copy after login

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.

  1. Rendering Template

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');
}
Copy after login

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.

  1. Run the test

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!