Skip to main content
Topic: PHPRad Api documentation (Read 5186 times) previous topic - next topic

PHPRad Api documentation

PHPRad API at a Glance

This document is intended to introduce you to the PHPRad framework and the principles of MVC architecture.
For full documentation please refer to this page https://phprad.com/info/api.


Application Entry Point (Index.php)
Mod_Rewrite (.htaccess)

By default Apache server has mod_rewrite enabled, if not mod_rewrite needs to be enabled.

The .htaccess contains the rules for the mod_rewrite which direct all request to the index page except if the request is an actual file or a directory. ()

Example:
Code: [Select]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request_uri=$1 [NC,L,QSA,B]

Autoloading
You don't want to manually include or require class file that we need in every script in the project, that's why PHP MVC frameworks have this autoloading feature. For example, if you put your own class file under 'lib' folder, then it will be auto loaded. that means you can instantiate the class and use it witout requiring the class file.
Code: [Select]
// Register Autoloaders
spl_autoload_register("autoloadModel");
spl_autoload_register("autoloadController");
spl_autoload_register("autoloadLibrary");
spl_autoload_register("autoloadHelper");

Routing
The Router translates URLs into controllers and actions and actions arguments.
PHPRad URLs
By default, URLs in PHPRad are designed to be search-engine and human readable. Rather than using the traditional query string approach to URLs that are used to build dynamic systems, PHPRad uses a path-based mechanism:

Code: [Select]
Examples:
http://localhost/myshop/controller/action/arg1/arg2/arg3/arg4/...

URI Segments
The segments in the URL, in following with the PHPRad MVC approach, usually represent:

controller/action/args/

The first segment represents the controller class that should be invoked.
The second segment represents the controller function, or method, that should be called.
The third and any additional segments represent the ID and any variables that will be passed to the controller function.
PHPRad Router Very Smart
No Need to create routers for individual pages or mess around with regular expression.
Various Examples of Routing. We are going to use a project called MyShop

Code: [Select]
To Load Product List Page
GET - http://localhost/myshop/products
or
GET - http://localhost/myshop/products/index
To Lr
GET - http://localhost/myshop/products/index
To Load Custom Product List Page
GET - http://localhost/myshop/products/active_products
To Load Product List Page By Property value
GET - http://localhost/myshop/products/category/plastics
or
GET - http://localhost/myshop/products/index/category/plastics
 
To Load a Product Detail Page
GET - http://localhost/myshop/products/2
or
GET - http://localhost/myshop/products/view/1
To Display Add Product Form Page
GET - http://localhost/myshop/products/add
To Save Product
POST - http://localhost/myshop/products/add
To Display Edit Product Form Page
GET - http://localhost/myshop/products/edit/3
To Update Product Changes
POST - http://localhost/myshop/products/edit/3oad Custom Product List Page
GET - http://localhost/myshop/products/active_products
To Load Product List Page By Property value
GET - http://localhost/myshop/products/category/plastics
or
GET - http://localhost/myshop/products/index/category/plastics
 
To Load a Product Detail Page
GET - http://localhost/myshop/products/2
or
GET - http://localhost/myshop/products/view/1
To Display Add Product Form Page
GET - http://localhost/myshop/products/add
To Save Product
POST - http://localhost/myshop/products/add
To Display Edit Product Form Page
GET - http://localhost/myshop/products/edit/3
To Update Product Changes
POST - http://localhost/myshop/products/edit/3


Controllers
PHPRad Controllers are of two categories
The System Controller (system/BaseController.php)
Application Controllers (app/controllers/*)
Base Controller
The Base Controller is stored in the system/BaseController.php directory.
The Base Controller contains two classes

BaseController class
This is the main controller class which other controllers can extend from.
SecureController class
This class also extends to the BaseController Class
Other application Controllers can also extend from this class if they are to be authenticated and authorize before executing any of the controller actions.
You don't need to add authorization code on all your controller actions if you extend to SecureController