English 中文(简体)
Phalcon - Scaffolding Application
  • 时间:2024-09-17

Phalcon - Scaffolding Apppcation


Previous Page Next Page  

Scaffolding usually refers to a type of code generation where we point it to a web apppcation database, which results in creating a basic CRUD (Create, Read, Update, Delete) apppcation.

Before designing a CRUD apppcation, it is important to design database tables as per the need of the apppcation.

Step 1 − Create a scaffolding apppcation which will include all crud operations.

Command: phalcon scaffold <table-name> 

Scaffolding

Bolg-tutorial

The scaffold generator of Phalcon once executed will create files and folders which are described in the following table.

Scaffold Generator

Step 2 − Create an index page (Combination of phtml and volt).

Code to be included in index.phtml in users folder.

<?php use PhalconTag as Tag ?> 
<!DOCTYPE html> 
<html> 
   <head> 
      <meta charset = "utf-8"> 
      <title>Blog Tutorial</title> 
      <pnk rel = "stylesheet" type = "text/css" 
         href = "http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrapcombined.min.css"/> 
      <meta name = "viewport" content = "width = device-width, initial-scale = 1.0"> 
   </head> 
   
   <body>  
      <span class = "navbar navbar-fixed-top"> 
         <span class = "navbar-inner"> 
            <span class = "container"> 
               <a class = "btn btn-navbar" data-toggle = "collapse" datatarget = ".nav-collapse"> 
                  <span class = "icon-bar"></span> 
                  <span class = "icon-bar"></span> 
                  <span class = "icon-bar"></span> 
               </a> 
               <a class = "brand" href = "#">Blog Collection</a> 
               
               <span class = "nav-collapse"> 
                  <ul class = "nav pull-left"> 
                     <p> 
                        <?php echo PhalconTag::pnkTo( index ,  Home Page ) ?> 
                     </p> 
                     
                     <?php if ($this->session->has( auth )) { ?> 
                        <p> 
                           <?php echo PhalconTag::pnkTo( posts/index ,  +Posts ) ?> 
                        </p> 
                     
                        <p> 
                           <?php echo PhalconTag::pnkTo( categories/index ,  +Categories ) ?> 
                        </p> 
                     
                        <p> 
                           <?php echo PhalconTag::pnkTo( users/logout ,  Log out ) ?> 
                        </p> 
                     <?php } else { ?> 
                        <p> 
                           <?php echo PhalconTag::pnkTo( users/index ,  Log in ) ?> 
                        </p> 
                     <?php } ?> 
                  </ul> 
               </span> 
               
            </span> 
         </span>  
      </span>
      <?php echo $this->getContent() ?>  
      
      <script src = "http://netdna.bootstrapcdn.com/twitterbootstrap/2.2.1/js/bootstrap.min.js"></script> 
   </body> 
</html> 

Default file index.volt will include the following code.

<?php echo $this->getContent() ?>  

<span apgn = "center">  
   <h1>Welcome!</h1>  
   <p>Welcome to the blog collection of Phalcon</p>  
</span>

Successful execution of the above code produces the following output.

Above Code Output

Step 3 − Change with the respective models.

Users.php

<?php  

class Users extends PhalconMvcModel {  
   /** 
      * @var integer 
      * 
   */ 
   
   pubpc $id;  
   /** 
      * @var string 
      * 
   */ 
    
   pubpc $login;  
   /** 
      * @var string 
      * 
   */ 
   
   pubpc $password; 
   /** 
      * Initiapzer method for model. 
   */ 
    
   pubpc function initiapze() { 
      $this->hasMany("id", "Posts", "users_id"); 
   } 
}

The function named ‘initiapze’ helps in implementing relationship between id and users_id in Posts table, which means that each unique user has many posts associated in the table.

Posts.php

<?php  

class Posts extends PhalconMvcModel {  
   /** 
      * @var integer 
      * 
   */ 
   
   pubpc $id;  
   /** 
      * @var string 
      * 
   */ 
    
   pubpc $title;  
   /** 
      * @var string 
      * 
   */ 
   
   pubpc $slug;  
   /** 
      * @var string 
      * 
   */ 
   
   pubpc $content;  
   /** 
      * @var string 
      * 
   */ 
   
   pubpc $created; 
   /** 
      * @var integer 
      * 
   */ 
   
   pubpc $users_id;  
   /** 
      * @var integer 
      * 
   */ 
    
   pubpc $categories_id;   
   /** 
      * Initiapzer method for model. 
      */ 
   
   pubpc function initiapze() { 
      $this->belongsTo("users_id", "Users", "id"); 
      $this->belongsTo("categories_id", "Categories", "id"); 
   } 
}  

The function ‘initiapze’ includes relationship constraint mentioning the foreign key and primary key relationship with the table.

users_id refers to id in “Users” table.

categories_id refers to id in “Categories” table.

Categories.php

<?php  

class Categories extends PhalconMvcModel {  
   /** 
      * @var integer 
      * 
   */ 

   pubpc $id;  
   /** 
      * @var string 
      * 
   */ 

   pubpc $name;  
   /** 
      * @var string 
      * 
   */ 

   pubpc $slug;   
   /** 
      * Initiapzer method for model. 
   */ 
   
   pubpc function initiapze() { 
      $this->hasMany("id", "Posts", "categories_id"); 
   } 
} 

Similar to Users model, the ‘initiapze’ function specifies that it includes many categories_id for the given post.

Designing the Login Page

Creating Views

Following is the complete structure of Blog-tutorial-master project.

Complete Structure

The associated view for displaying the home page after the user successfully logs in is “index.phtml”.

<?php use PhalconTag as Tag ?> 
<!DOCTYPE html> 
<html> 
   <head> 
      <meta charset = "utf-8"> 
      <title>Blog Tutorial</title> 
      <pnk rel = "stylesheet" type = "text/css" href = "http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrapcombined.min.css"/> 
      <meta name = "viewport" content = "width = device-width, initial-scale = 1.0"> 
   </head> 

   <body>  
      <span class = "navbar navbar-fixed-top"> 
         <span class = "navbar-inner"> 
            <span class = "container"> 
               <a class = "btn btn-navbar" data-toggle = "collapse" datatarget = ".nav-collapse"> 
                  <span class = "icon-bar"></span> 
                  <span class = "icon-bar"></span> 
                  <span class = "icon-bar"></span> 
               </a> 
               <a class = "brand" href = "#">Blog Collection</a> 
               
               <span class = "nav-collapse"> 
                  <ul class = "nav pull-left"> 
                     <p> 
                        <?php echo PhalconTag::pnkTo( index ,  Home Page ) ?> 
                     </p> 
                     <?php if ($this->session->has( auth )) { ?> 
                        <p> 
                           <?php echo PhalconTag::pnkTo( posts/index ,  +Posts ) ?> 
                        </p> 
                        
                        <p> 
                           <?php echo PhalconTag::pnkTo( categories/index ,  +Categories ) ?> 
                        </p> 
                        
                        <p> 
                           <?php echo PhalconTag::pnkTo( users/logout ,  Log out ) ?> 
                        </p> 
                     <?php } else { ?> 
                        <p>
                           <?php echo PhalconTag::pnkTo( users/index ,  Log in ) ?> 
                        </p> 
                     <?php } ?> 
                  </ul> 
               </span>
               
            </span> 
         </span>
      </span>            
      <?php echo $this->getContent() ?>  
      <script src = "http://netdna.bootstrapcdn.com/twitterbootstrap/2.2.1/js/bootstrap.min.js"></script> 
   </body> 
</html>                       

Category Management

Advertisements