- Phalcon - Security Features
- Phalcon - Object Document Mapper
- Phalcon - Working with Forms
- Phalcon - Asset Management
- Phalcon - Multi-Lingual Support
- Phalcon - Session Management
- Phalcon - Cookie Management
- Phalcon - Database Migration
- Phalcon - Query Language
- Phalcon - Scaffolding Application
- Phalcon - Switching Databases
- Phalcon - Database Connectivity
- Phalcon - Routing
- Phalcon - Views
- Phalcon - Models
- Phalcon - Controllers
- Phalcon - Configuration
- Phalcon - Functionality
- Phalcon - Application Structure
- Phalcon - Environmental Setup
- Phalcon - Overview
- Phalcon - Home
Phalcon Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Phalcon - Scaffolding Apppcation
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>
The scaffold generator of Phalcon once executed will create files and folders which are described in the following table.
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.
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.
Creating Views
Following is the complete structure of Blog-tutorial-master project.
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>
Advertisements