Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Quickstart


Documentation

Quick Start

  • Install and Configuration
  • Creating Controller
  • Creating Model
  • Creating View
  • Routing
  • Display Data in View

Install and Configuration

Before starting you need to be sure that you have installed Cygnite Framework into the machine. If you haven't already, you can find installation steps in Cygnite Framework Installation Procedure.

Creating Controller

To get started, there are sample controller available into the src/Apps/Controllers directory. You can also create controller.

    
namespace Apps\Controllers;

use Cygnite\Foundation\Application;
use Cygnite\Mvc\Controller\AbstractBaseController;

class UsersController extends AbstractBaseController
{
   public function __construct()
   {
       parent::__construct();
   }

   public function indexAction()
   {
      // using php layout and view
     $view = View::create('Apps.Views.home.index', ['name'=> 'Cygnite PHP Framework']);

     return Response::make($view);
   }
}

Creating Model

Cygnite Skeleton application also shipped with sample model into your src/Apps/Models/ directory. You can create your model in same directory to interact with your database. Your model class name should be StudlyCaps, every model class act as simple ORM to map your database tables as objects, Cyrus model follows ActiveRecord style highly inspired by ruby on rails. Your table name must be lowercase followed by underscore prefix.

Example- If your table name is guest_book than your model name should be GuestBook.

    
namespace Apps\Models;

use Cygnite\Database\Schema;
use Cygnite\Common\UrlManager\Url;
use Cygnite\Database\Cyrus\ActiveRecord;

class GuestBook extends ActiveRecord
{
   /**
    * Your database name goes here
    * @param database 
    */
    protected $database = 'cygnite';

   /**
    * @param $tableName
    * Optional. If you don't provide table name here class will act as database table.
    */
    //protected $tableName = 'guest_book'; 

   /**
    * Primary key of your table
    * We recommend to have id as primary key
    */
    protected $primaryKey = 'id';

    public function __construct()
    {
        parent::__construct();
    }
}

Creating View

Next, let's create simple view to display our welcome page which is reside in src/Apps/Views/home directory. Cygnite gives you flexibility to use either plain PHP layout or twig template engine. You are allowed to use widget to render partial views. For Example:

 
    <html>
    <body>
        <h1>Welcome to Cygnite Framework QuickStart </h1>
            Congrats !! You have created your first view page.
    </body>
</html>

You can also create template layout and render the sections into it. Cygnite Framework has built in support for most elegant and powerful twig template engine. For more details check Views

Routing

Cygnite provides powerful routing mechanism. You may use src/Apps/Routing/routes.php for adding routes to your application. It allows you to write powerful rest API using simple closure object or static routing to controller. Router also gives you flexibility to call controller's action with arguments from your routes.

Let us take a look at example:

The src/Apps/Routing/routers.php has closure support to build powerful REST API. You can define all routes here.



    // Before Router Middleware
    $app->router->get('home/', function() {
          return "Welcome! to Cygnite PHP Framework";
    });

For more details documentation please have a look at Routing guide.

Displaying Data Into View

Let us see how to display data into view. If you are using plain php view page you should pass second parameter as to render function and in the case of twig template engine you should use render method.



     // using php layout and view
     $view = View::create('Apps.Views.home.index', ['name'=> 'Cygnite PHP Framework']);

     // If you use twig template engine
     $this->render('home.index', [
                      'author'=>$this->author,
                      'messege' => 'Welcome to Cygnite Framework',
                      ]);

You need to assign values to view as by simply passing values to with function. You can get the data into the view (if plain php file used as view) or in layout as follows.

    
  {{author}} //twig templates
  echo $email; // plain PHP view page

Generating Crud

Craft console helps to simplify your job. You may generate code using craft console. Read more: Cygnite CLI - Crud Generator


Follow Us On Facebook Twitter Google+ Linkedin
Released Under The MIT Public License. Copyrights @2012-2017. Powered by- Sanjoy Dey Productions.