Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CakePHP small application and understanding MVC Architecture

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 107
    Comment on it

    Hello Readers, Here is a small application in CakePHP to add users through which we can Understand "How MVC Architecture works" ? For better understanding of MVC Architecture, I will manually create Model, Controller and Views.

    First, You have to create a table in DB to store user data.
     

    CREATE TABLE `users` (
    
      `id` INTEGER(11) NOT NULL AUTO_INCREMENT,
    
      `name` VARCHAR(255) NOT NULL,
    
      PRIMARY KEY (`id`)
    
    )

    Table names are in plural. Since, it is important to follow CakePHP naming conventions. In the previous tutorial, I explained some basics about CakePHP naming Conventions.

     

    MODEL:

    Now, i will create a model for above users table. CakePHP models are located at (Path: /app/models folder). create a file called user.php & paste the following code into it.

    <?php
    
    class User extends AppModel {
    
        var $name = 'User';
    
    }
    
    ?>

    $name = this variable will be used in our Controller to access the model’s functions.

     

    CONTROLLER:

    CakePHP controllers are located at (Path: /app/controllers folder). create a file called user_controller.php & pase the following code into it.

    <?php
    
    class UsersController extends AppController {
    
    
        var $name = 'User';
    
    
        function index() {
    
            $this->set('users', $this->User->find('all'));
    
        }
    
    
           function add() {
    
            if (!empty($this->data)) {
    
                if ($this->User->save($this->data)) {
    
                    $this->Session->setFlash('User has been saved.');
    
                    $this->redirect(array('action' => 'index'));
    
                }
    
            }
    
    }
    
    ?>

    VIEW:

    CakePHP view files are stored at /app/views/folder_name correspond to controller (we will have to create a folder named users in this case). Create a file index.ctp in /app/views/users folder & Copy/paste the following code into index.ctp
     

    <h1>Users</h1>
    
    <table>
    
        <tr>
    
            <th>Id</th>
    
            <th>Title</th>
    
        </tr>
    
    
        <?php foreach ($users as $user): ?>
    
        <tr>
    
            <td><?php echo $user['User']['id']; ?></td>
    
            <td>
    
                <?php echo $html->link($user['User']['name'],
    
    array('controller' => 'users', 'action' => 'view', $user['User']['id'])); ?>
    
            </td>
    
        </tr>
    
        <?php endforeach; ?>
    
    </table>

    To test what you have done, Go to command terminal-> Run the server.

    then, Open web browser & hit the following URL http://localhost/cake/users)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: