Cakephp 3.0 | Steps to create theme controller

|
| By Webner

Description : Cakephp 3.0 provides a way to display different front end view for different clients using Themes. Theme is a plugin which follows the same structure as other cakephp plugins. Before Cakephp 3.0 it was only used to separate front end view for the clients. But in Cakephp 3.0 it provides us more flexibility to write business logic inside Theme plugin.

For example, we implemented admin section of our cakephp site using Theme plugin in our project. We wrote separate business logic specific to admin section inside Admin plugin. Now it is totally independent from rest of the project and we can easily move it to the other location without changes.

So implementing theme as a plugin is a nice feature of cakephp 3.0.

Follow these steps before writing controller code inside theme plugin :

Step 1: Go to bootstrap.php file in your project. Bootstrap file is located inside root folder -> config->bootstrap.php.

Append this code to the bootstrap.php file:

Plugin::load(‘plugin name’ , [‘bootstrap’ => false, ‘routes’ => true,’autoload’ => true]);

As plugin name is “Admin” , use the following syntax inside bootstrap.php file.

Plugin::load('Admin', ['bootstrap' => false, 'routes' => true,'autoload' => true]);

Pass following three parameters:

a. Bootstrap = false

If your plugin has bootstrap.php file but the requirement is to use main project bootstrap.php file instead of plugin bootstrap.php file, pass false value.

b. routes =true:

If you want to use your plugin routes.php file then enter yes.

c. autoload =true:

If you want to load your plugin automatically during startup of your project.
Then pass true value.

Step 2: Create routes.php file inside your plugin configuration folder. The contents of the routes.php file is as follow:

<?php
use Cake\Routing\Router;

Router::plugin(
 'Admin',
 ['path' => '/admin'],
 function ($routes) 
  {
    $routes->fallbacks('DashedRoute');
  }
);

In the above piece of code, path => admin is the main thing as it is the identifier used to find controller inside theme plugin instead of root folder. Now “admin” word will show up in the url.

For example: http://localhost/cakephpTestProejct/admin/login

Step 3: Use namespace in controller as below to differentiate it:

<?php
namespace Admin\Controller;
use Admin\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Http\Client;
class LoginController extends AppController
{
// write your logic here.
}

Normally the name space in cakephp 3.0 is namespace App\Controller; but for plugin folder, just change the name space as per your plugin.

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at webdevelopment@webners.com

Leave a Reply

Your email address will not be published. Required fields are marked *