CakePHP / Basics / CakePHP new project
New Project
-
STEPS
1. Installing Composer
2. Create a CakePHP Project
open the php.ini file and add
extension=php_intl.dll composer create-project --prefer-dist cakephp/app:~4.0 my_app_name 3. Folder structure
bin config resources src templates vendor webroot index.php 4. index file
templates/Pages/home.php 5. Routing
config/routes.php file:
$builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']); 6. Controllers
Folder path: src/Controller/
namespace App\Controller; use Cake\Controller\Controller; class AppController extends Controller { } Controller
render view page// src/Controller/RecipesController.php class RecipesController extends AppController { public function view($id) { // Action logic goes here. } public function share($customerId, $recipeId) { // Action logic goes here. } public function search($query) { // Action logic goes here. } } return $this->render('home'); 7. Views
templates folder
8. DB connection
config/app_local.php
'host' => 'localhost', 'username' => 'my_app', 'password' => 'secret', 'database' => 'my_app', 9. Table Objects
src/Model/Table.
In controller// src/Model/Table/ArticlesTable.php namespace App\Model\Table; use Cake\ORM\Table; class ArticlesTable extends Table { public function initialize(array $config): void { $this->setTable('my_table'); $this->setPrimaryKey('my_id'); } } $articles = $this->getTableLocator()->get('Articles');