Now a days, there are several PHP frameworks where we work. But from those available frameworks, we select some best ones. CakePHP framework comes in the list of best frameworks of PHP. Inside this article we will learn CakePHP 4 step by step which covers all topics of CakePHP 4 basics.
Simply, if any one asks from us What is a PHP framework ? What should we reply to this query, any idea.
In simple terms you have reply, the collection of code, packages, classes etc which in turns make work easy and development faster using PHP language called a PHP framework.
Learn More –
- HTML Helper in CodeIgniter 4 Tutorial
- HTTP CURL Request Service in CodeIgniter 4 Tutorial
- HTTP Method Spoofing in CodeIgniter 4
- Image Manipulation Class in CodeIgniter 4 Tutorial
Let’s get started about CakePHP 4 Basics in detailed concept.
What is CakePHP 4.x ?
CakePHP is an open source framework based on PHP Language. It has been created to build/develop application faster – by means of it’s core libraries, packages, modules etc. Here are some CakePHP features –
- MVC supported – It supports MVC pattern to code methodology. M stands for Model, V stands for View & C stands for Controller.
- By using CakePHP in application development – it turns development in faster mode.
- It will installed by using method of composer.
- Provide Bake console commands, code generation techniques etc.
- It supports multiple database supports like MySQL, Postgres etc.
- It provides ease concept of template engines.
4.x is the latest version of CakePHP when we are writing this article. So inside this article we will cover the development basics of CakePHP 4.
CakePHP 4 Installation
To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.
$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp
Above command will creates a project with the name called mycakephp.
Inside this command we can also see, we have used composer and cakephp v 4.0. Have a look at this image about the installation of cakephp via terminal –
Installing packages and modules as we can see clearly.
Successfully, we have done the installation.
Open Application to Browser
Back to installed project folder >> copy the URL >> Open browser >> Paste & go.
When you see this screen after running it means you did everything in perfect way. If you are getting some error or bit distorted layout other than this it means some issues happened at installation.
Understanding Over the Application Landing Page
As we have opened landing page of the application above, this is the application setup of CakePHP 4 default page. For this page, application have 1 default route, 1 controller and sure it will have 1 view file as well.
Let’s search all those files and see where they are in setup –
Application Route
Routes are configured inside cakephp application is at /config/routes.php. This routes.php file is responsible to handle all request routes including post, get, delete etc.
For default landing page, the routes is –
$builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
Inside this given route, we can see it is calling a Pages controller and method inside Page Controller is display and the view file it is calling as home.php
Application Controller
Controllers are stored inside /src/Controller/PagesController.php. AppController is the parent controller of any in CakePHP 4.
class PagesController extends AppController {}
Application View
All view files will be stored inside /templates directory. We will see more in folder structure. For landing page, we can find the view file at /templates/Pages/home.php. Here, Pages is the name of the controller.
About Folder Structure in details
Application structure contains many working folders that we should know before starting development.
These are the folders come when we install CakePHP via composer. It contains /bin /config /logs /plugins etc.
Details
/bin – This folder contains console executable files.
/config – Main configuration folder which stores the config for database, routes, paths, bootstrap cli etc.
/log – Name itself clears that folder that contains application log files. Log files are dependent on the config for log.
/plugins – When we need to use third party modules, libraries, SDKs then we need to place those folders here inside this plugins folder.
/resources – It contains resources of application like let’s say application supports multiple language, then we can store language related files here.
/src – This one will be the main folder of application where source code of project will be stored.
It contains folders of Command, Controllers, Console, Model, View
/templates – Inside this folder, we store view files of applications. Additionally, we store layouts, partials files, email templates, UI, error pages etc in this.
/webroot – Application files like css, js, images or any other public accessible files will be there.
/vendor – Vendor folder stores all the dependencies files which we have installed via composer. Means when we installed cakephp via composer, then composer downloaded files are here inside this.
CakePHP 4.x Files – Naming Convention
When we work with any framework, in most cases you will find that there will some naming conventions to work with that. So, accordingly here we have also the naming convention inside this CakePHP 4.x.
This naming convention includes the file names, class names, database table name etc about those concept. Let’s see about those in details –
Controller Name
When we create any controller in application, then we need to keep these things in mind. Controller class name should be plural, camel cased and must end with Controller keyword.
Might have some confusion about the rule, let’s clear that –
- Plural – Instead of using Student, it should be Students.
- Camel Case – Connecting words must be camel cased meaning Studentclass should be like this StudentClass. Here we can see Student and Class are two different words and they are connected.
- End with Controller – Means controller class name should end with Controller keyword.
Combine all the things what we have seen, Examples –
StudentsController, StudentReportsController etc.
Methods Name
Methods are the controllers functions. To define these methods we have also a naming format for this. These are camel backed.
Examples – report(), reportDownload()
Routes Name
Routes can be configured in routes.php file, as we have discussed previously. While defining routes, it depends on method names.
Example –
- report() of SitesController, Route will be /site/report
- reportDownload() of SitesController, Route will be /site/report-download
View Files
These are the view files which we call in methods of controller to return data.
Example –
- report() of SitesController, Route will be /Sites/report.php
- reportDownload() of SitesController, Route will be /Sites/report_download.php
Model Naming Convention
Models are the database functional files which contains – Table Class & an Entity Class. So, here we have naming conventions for Table class and Entity class.
Table Class should be camel cased, plural and must be end with Table keyword.
Entity Class should be singular only.
Examples –
Table Class – UsersTable.php, UserDatasTable.php
Entity class – User.php, Student.php
How can we create a Controller in CakePHP 4 application ?
Controllers are the logical files in application. Concept of Controllers are very important in case of MVC pattern. C is nothing, it’s controller. It acts as a bridge in working principle with Model & Views. Also it returns the output to every request which comes in application.
Controllers are stored inside /src/Controller directory.
Let’s see the syntax to create –
<?php namespace App\Controller; use App\Controller\AppController; class ProjectsController extends AppController { public function initialize(): void { // Construtor of this Class parent::initialize(); } public function myMethod1(){ // This is method 1 of this controller } }
Here, we have developed a ProjectsController inside /src/Controller/ProjectsController.php. As we know, AppController is the parent controller of every controllers in CakePHP, so we need to extends because of using core features.
Inside Controller class, we have defined constructor of the class i.e initialize() method. Inside this initialize(), we load our layout, models etc. Also we defined a test method called myMethod1().
Creating a View file for Controller Method
View files are the presentation layer to users. We can create view files at /templates directory. As we have myMethod1() at previously given controller ProjectController. So, let’s understand the concept of view by creating a view file for it.
Go to /templates folder. Create a folder with the name of Controller class i.e Projects. So, now we have /templates/Projects.
Inside Project folder, we will create /Projects/my_method1.php view file. View file will be automatically called by controller behind the scene.
Route Configuration of Application
As, we already discussed i.e CakePHP 4 routes can be configured inside /config/routes.php file. So, for above method, we have created /method1 as our route.
//... $builder->connect("/method1", ["controller" => "Projects", "action" => "myMethod1"]); //...
Wrapping all things together –
We hope this article helped you to learn about CakePHP 4 Basics Working Concept Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.