Installing Cakephp 3
Welcome to Findnerd friends. Today I am going to tell you how to generate code using cakephp 3.
We are going to start from the installation process in cakephp 3. Lets begin installing cakephp 3 by writing the following command on the terminal:
$ sudo composer self-update && composer create-project --prefer-dist cakephp/app cakephp3_project
*Note: Following must be installed on your system to install cakephp3
i) PHP 5.5.9 or greater
ii) intl extension (this can be installed by using the command): sudo apt-get install php5-intl
iii) mbstring extension
When cakephp 3 is installed, then configure your database connection by going to the directory: config/app.php.
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '******',
'database' => 'databasename',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
]
],
Now go the the browser and run the url : http://localhost/cakephp3_project. Now you can see the project is running.
Code Generation with Bake
The most powerful thing supported by cakephp is baking, through which we can generate controllers, models and views.
Note: Before beginning bake you must configure your database connection correctly and then run the bake command on terminal for successful execution.
To start running the bake command, you need to go to the cakephp3_project directory by writing the command on the terminal:
$ cd /var/www/html/cakephp3_project
Now run the bake command by typing:
$ sudo bin/cake bake
Now you will see the following output:
Welcome to CakePHP v3.2.13 Console
---------------------------------------------------------------
App : src
Path: /var/www/html/cakephp3_project/src/
PHP : 5.5.9-1ubuntu4.19
---------------------------------------------------------------
The following commands can be used to generate skeleton code for your application.
Available bake commands:
- all
- behavior
- cell
- component
- controller
- fixture
- form
- helper
- mailer
- migration
- migration_diff
- migration_snapshot
- model
- plugin
- seed
- shell
- shell_helper
- task
- template
- test
By using `cake bake [name]` you can invoke a specific bake task.
You can see the list of commands available here for bake. If you want to bake all, then you need to enter following command:
$ sudo bin/cake bake all
This command will show you list of tables available for baking. Like here I am getting the list of the following tables after running the above command:
Welcome to CakePHP v3.2.13 Console
---------------------------------------------------------------
App : src
Path: /var/www/html/cakephp3/src/
PHP : 5.5.9-1ubuntu4.19
---------------------------------------------------------------
Bake All
---------------------------------------------------------------
Possible model names based on your database:
- banners
- cities
- client_categories
- client_consultant_communications
- client_departments
- client_reports
- consultant_attachments
- consultant_categories
- contacts
- contents
- event_participants
- event_registrations
- event_transactions
- events
- faqs
- galleries
- permissions
- states
- subscription_transactions
- subscriptions
- task_transactions
- tasks
- testimonials
- user_details
- users
Run `cake bake all [name]` to generate skeleton files.
As you can see the list of tables available to perform the bake operation. Now you can bake any one of the tables that you wish to bake. As I want to bake users table first, so I am going to write the command:
$ sudo bin/cake bake all users
This command will generate Userscontroller.php in src/Controller directory, User.php in src/Model/Entity
directory, UsersTable.php in src/Model/Table
directory and (add.ctp, edit.ctp, index.ctp, view.ctp) in src/Template/Users
directory.
Above command will generate the following output:
Welcome to CakePHP v3.2.13 Console
---------------------------------------------------------------
App : src
Path: /var/www/html/cakephp3_project/src/
PHP : 5.5.9-1ubuntu4.19
---------------------------------------------------------------
Bake All
---------------------------------------------------------------
One moment while associations are detected.
Baking table class for Users...
Creating file /var/www/html/cakephp3_project/src/Model/Table/UsersTable.php
Wrote `/var/www/html/cakephp3_project/src/Model/Table/UsersTable.php`
Deleted `/var/www/html/cakephp3_project/src/Model/Table/empty`
Baking entity class for User...
Creating file /var/www/html/cakephp3_project/src/Model/Entity/User.php
Wrote `/var/www/html/cakephp3_project/src/Model/Entity/User.php`
Deleted `/var/www/html/cakephp3_project/src/Model/Entity/empty`
Baking test fixture for Users...
Creating file /var/www/html/cakephp3_project/tests/Fixture/UsersFixture.php
Wrote `/var/www/html/cakephp3_project/tests/Fixture/UsersFixture.php`
Deleted `/var/www/html/cakephp3_project/tests/Fixture/empty`
Bake is detecting possible fixtures...
Baking test case for App\Model\Table\UsersTable ...
Creating file /var/www/html/cakephp3_project/tests/TestCase/Model/Table/UsersTableTest.php
Wrote `/var/www/html/cakephp3_project/tests/TestCase/Model/Table/UsersTableTest.php`
Baking controller class for Users...
Creating file /var/www/html/cakephp3_project/src/Controller/UsersController.php
Wrote `/var/www/html/cakephp3_project/src/Controller/UsersController.php`
Bake is detecting possible fixtures...
Baking test case for App\Controller\UsersController ...
Creating file /var/www/html/cakephp3_project/tests/TestCase/Controller/UsersControllerTest.php
Wrote `/var/www/html/cakephp3_project/tests/TestCase/Controller/UsersControllerTest.php`
Baking `index` view template file...
Creating file /var/www/html/cakephp3_project/src/Template/Users/index.ctp
Wrote `/var/www/html/cakephp3_project/src/Template/Users/index.ctp`
Baking `view` view template file...
Creating file /var/www/html/cakephp3_project/src/Template/Users/view.ctp
Wrote `/var/www/html/cakephp3_project/src/Template/Users/view.ctp`
Baking `add` view template file...
Creating file /var/www/html/cakephp3_project/src/Template/Users/add.ctp
Wrote `/var/www/html/cakephp3_project/src/Template/Users/add.ctp`
Baking `edit` view template file...
Creating file /var/www/html/cakephp3_project/src/Template/Users/edit.ctp
Wrote `/var/www/html/cakephp3_project/src/Template/Users/edit.ctp`
Bake All complete.
Now you can see the output on the browser: http://localhost/cakephp3_project/users/
Thanks for reading.
0 Comment(s)