Home Backend Development PHP Tutorial How to use Blade with CakePHP?

How to use Blade with CakePHP?

Jun 04, 2023 am 10:01 AM
Instructions cakephp blade

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

How to use DirectX repair tool? Detailed usage of DirectX repair tool How to use DirectX repair tool? Detailed usage of DirectX repair tool Mar 15, 2024 am 08:31 AM

The DirectX repair tool is a professional system tool. Its main function is to detect the DirectX status of the current system. If an abnormality is found, it can be repaired directly. There may be many users who don’t know how to use the DirectX repair tool. Let’s take a look at the detailed tutorial below. 1. Use repair tool software to perform repair detection. 2. If it prompts that there is an abnormal problem in the C++ component after the repair is completed, please click the Cancel button, and then click the Tools menu bar. 3. Click the Options button, select the extension, and click the Start Extension button. 4. After the expansion is completed, re-detect and repair it. 5. If the problem is still not solved after the repair tool operation is completed, you can try to uninstall and reinstall the program that reported the error.

Introduction to HTTP 525 status code: explore its definition and application Introduction to HTTP 525 status code: explore its definition and application Feb 18, 2024 pm 10:12 PM

Introduction to HTTP 525 status code: Understand its definition and usage HTTP (HypertextTransferProtocol) 525 status code means that an error occurred on the server during the SSL handshake, resulting in the inability to establish a secure connection. The server returns this status code when an error occurs during the Transport Layer Security (TLS) handshake. This status code falls into the server error category and usually indicates a server configuration or setup problem. When the client tries to connect to the server via HTTPS, the server has no

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

See all articles