Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cake bake in Cakephp 3

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.43k
    Comment on it

    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:

     

    1. $ 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.

     

    1. 'Datasources' => [
    2. 'default' => [
    3. 'className' => 'Cake\Database\Connection',
    4. 'driver' => 'Cake\Database\Driver\Mysql',
    5. 'persistent' => false,
    6. 'host' => 'localhost',
    7. 'username' => 'root',
    8. 'password' => '******',
    9. 'database' => 'databasename',
    10. 'encoding' => 'utf8',
    11. 'timezone' => 'UTC',
    12. 'cacheMetadata' => true,
    13. ]
    14. ],

     

    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:

     

    1. $ cd /var/www/html/cakephp3_project

     

    Now run the bake command by typing:

     

    1. $ sudo bin/cake bake

     

    Now you will see the following output:

     

    1. Welcome to CakePHP v3.2.13 Console
    2. ---------------------------------------------------------------
    3. App : src
    4. Path: /var/www/html/cakephp3_project/src/
    5. PHP : 5.5.9-1ubuntu4.19
    6. ---------------------------------------------------------------
    7. The following commands can be used to generate skeleton code for your application.
    8.  
    9. Available bake commands:
    10.  
    11. - all
    12. - behavior
    13. - cell
    14. - component
    15. - controller
    16. - fixture
    17. - form
    18. - helper
    19. - mailer
    20. - migration
    21. - migration_diff
    22. - migration_snapshot
    23. - model
    24. - plugin
    25. - seed
    26. - shell
    27. - shell_helper
    28. - task
    29. - template
    30. - test
    31.  
    32. By using `cake bake [name]` you can invoke a specific bake task.
    33.  

     

    You  can see the list of commands available here for bake. If you want to bake all, then you need to enter following command:

     

    1. $ 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:

     

    1. Welcome to CakePHP v3.2.13 Console
    2. ---------------------------------------------------------------
    3. App : src
    4. Path: /var/www/html/cakephp3/src/
    5. PHP : 5.5.9-1ubuntu4.19
    6. ---------------------------------------------------------------
    7. Bake All
    8. ---------------------------------------------------------------
    9. Possible model names based on your database:
    10. - banners
    11. - cities
    12. - client_categories
    13. - client_consultant_communications
    14. - client_departments
    15. - client_reports
    16. - consultant_attachments
    17. - consultant_categories
    18. - contacts
    19. - contents
    20. - event_participants
    21. - event_registrations
    22. - event_transactions
    23. - events
    24. - faqs
    25. - galleries
    26. - permissions
    27. - states
    28. - subscription_transactions
    29. - subscriptions
    30. - task_transactions
    31. - tasks
    32. - testimonials
    33. - user_details
    34. - users
    35. Run `cake bake all [name]` to generate skeleton files.
    36.  

     

    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:

     

    1. $ 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:

    1. Welcome to CakePHP v3.2.13 Console
    2. ---------------------------------------------------------------
    3. App : src
    4. Path: /var/www/html/cakephp3_project/src/
    5. PHP : 5.5.9-1ubuntu4.19
    6. ---------------------------------------------------------------
    7. Bake All
    8. ---------------------------------------------------------------
    9. One moment while associations are detected.
    10.  
    11. Baking table class for Users...
    12.  
    13. Creating file /var/www/html/cakephp3_project/src/Model/Table/UsersTable.php
    14. Wrote `/var/www/html/cakephp3_project/src/Model/Table/UsersTable.php`
    15. Deleted `/var/www/html/cakephp3_project/src/Model/Table/empty`
    16.  
    17. Baking entity class for User...
    18.  
    19. Creating file /var/www/html/cakephp3_project/src/Model/Entity/User.php
    20. Wrote `/var/www/html/cakephp3_project/src/Model/Entity/User.php`
    21. Deleted `/var/www/html/cakephp3_project/src/Model/Entity/empty`
    22.  
    23. Baking test fixture for Users...
    24.  
    25. Creating file /var/www/html/cakephp3_project/tests/Fixture/UsersFixture.php
    26. Wrote `/var/www/html/cakephp3_project/tests/Fixture/UsersFixture.php`
    27. Deleted `/var/www/html/cakephp3_project/tests/Fixture/empty`
    28. Bake is detecting possible fixtures...
    29.  
    30. Baking test case for App\Model\Table\UsersTable ...
    31.  
    32. Creating file /var/www/html/cakephp3_project/tests/TestCase/Model/Table/UsersTableTest.php
    33. Wrote `/var/www/html/cakephp3_project/tests/TestCase/Model/Table/UsersTableTest.php`
    34.  
    35. Baking controller class for Users...
    36.  
    37. Creating file /var/www/html/cakephp3_project/src/Controller/UsersController.php
    38. Wrote `/var/www/html/cakephp3_project/src/Controller/UsersController.php`
    39. Bake is detecting possible fixtures...
    40.  
    41. Baking test case for App\Controller\UsersController ...
    42.  
    43. Creating file /var/www/html/cakephp3_project/tests/TestCase/Controller/UsersControllerTest.php
    44. Wrote `/var/www/html/cakephp3_project/tests/TestCase/Controller/UsersControllerTest.php`
    45.  
    46. Baking `index` view template file...
    47.  
    48. Creating file /var/www/html/cakephp3_project/src/Template/Users/index.ctp
    49. Wrote `/var/www/html/cakephp3_project/src/Template/Users/index.ctp`
    50.  
    51. Baking `view` view template file...
    52.  
    53. Creating file /var/www/html/cakephp3_project/src/Template/Users/view.ctp
    54. Wrote `/var/www/html/cakephp3_project/src/Template/Users/view.ctp`
    55.  
    56. Baking `add` view template file...
    57.  
    58. Creating file /var/www/html/cakephp3_project/src/Template/Users/add.ctp
    59. Wrote `/var/www/html/cakephp3_project/src/Template/Users/add.ctp`
    60.  
    61. Baking `edit` view template file...
    62.  
    63. Creating file /var/www/html/cakephp3_project/src/Template/Users/edit.ctp
    64. Wrote `/var/www/html/cakephp3_project/src/Template/Users/edit.ctp`
    65. Bake All complete.
    66.  

     

    Now you can see the output on the browser: http://localhost/cakephp3_project/users/

     

    Thanks for reading.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: