install/leggo

There is no license information available for the latest version (dev-master) of this package.

A MVC based framework for web artist

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Language:HTML

dev-master 2019-05-25 06:31 UTC

This package is not auto-updated.

Last update: 2024-05-02 15:41:24 UTC


README

A MVC based framework written in php

Table of content

  • About Leggo
  • Installing Leggo
  • Architecture of leggo framework
  • Working of leggo
  • Directory Hierarchy of leggo
  • Defining your web route
  • Creating a controller for you web route
  • Creating a view
  • Creating a model
  • Database:Migrations
  • What is database migration
  • Generating a migration
  • Migration structur
  • Running a migration
  • Database:Seeders
  • what is seeders
  • Writing a seeder
  • Running a seeder
  • Framework dependencies

About Leggo

Leggo is a lite MVC based framework which is written in PHP.It is comes with a CLI client to faciliate the rapid development. It is for developers who want to customize the code without worring about what undergoing the hood. Leggo provide you a simple directory hierarchy which you can work with easily. Unlike another heavy weight framework like Laravel, symphony, cakePHP there are not lot of files included in a a single script. A developers can easily figure out which framework file is used by the current scipt/code.The main advantage of this is customization. Now when developers know what going in background they can easily customize the code according to them self. By default a data abstraction layer is provided to intract with database and handle all the sql transactions dynamically. If a developers want to integrate ORM they can integrate ORM of there choice. Leggo uses the concept of template, we can define a template for particular route in templates directory. To access the template we only have to provide a $template_name variable with the the name of template .Currently it did not uses any template engine . But in later version of leggo developers can eaisly integrate template engine of there choice. Leggo is a pure MVC based framework in which all the data is handle by its views . It provide the power to develop a powerfull, scalable yet secure webapp without hassling with logic part. It facilitate its user to integrate ORM(object relational mapper ) like propel , doctrine etc and template engines of there choice. It did not force user to use any particular

Installing Leggo

You can download leggo to your machine by two ways

Install from github

To install run following command into your git bash shell

$ > git clone https://github.com/thecodestuff/leggo.git

Install using package manager

To install using composer run following command

$ > require install/leggo

Architecture of leggo framework

artictech image here

Leggo uses MVC design pattern under the hood.MVC stand for model view controller. In a typical MVC framework all the logic is handle by the controller, database interaction is handled by the its respective model and all the front-end code is stored in its respective template which is fetched by its respective view.

Working of leggo

In leggo all start with defining a web route in vendor/app/route/web.php. our .htaccess is configured in a such a way that every request from the client is route to the index.php file in root directory. Index file call the in which all web route are written.Framework checks weather a web route/url is valid or not. If request is not valid a error message is shown to the user else controller is evoked by the leggo. Once controller is evoked its view and model are also evoked. View check for the template mention in $template_name variable in view class in leggo/template directory.

Directory Hierarchy of leggo

dh.PNG

Defining your web route

A web is a URL which evokes a particuallar controller . To define a web route navigate to /vendor/route/web.php file

Route::get('helloworld/' , 'HelloWorldController@index') ;

Route::get function accept to parameter .First parameter is the name of your route and the second parameter is the name of controller and the name of function.

Creating a controller for you web route

Leggo comes with its CLI tool, using this you can create controller file for your route. To create a controller file use below command.

$ > php console make::controller HelloWorldController

navigate to your /vendor/app/controllers directory you will find that your controller created with some code already added for you to get start with .

Your typicall controller file looks like this

<?php 
class HelloWorldController extends controller{
	public $view  ;
	/**
	 * evoke repective view for this controller and uri
	 */
	public function index(){
		# initilizing view to work with view class
		$this->view = new HelloWorldView ;
		$this->view->output() ;

	}
}
?>

Here HelloWorldController class extends to base controller class. Base controller hava a function name view() which evokes the view of particullar route A typicall controller.php file look like this

<?php 
# This is a base controller 
# Its functionality can be accessed by other controllers 

class controller{
	public static function view($viewName ){
		require $_SERVER['DOCUMENT_ROOT'].'/live tracking/vendor/app/view/'.$viewName.'.php' ;
		
	}

}

?>

Now you have created a controller for your web route , its time to create a view for your controller

Creating a view

A view contain a logic which enable user to transport the data fetch by the model from the database to the template. It work in hand with templates which are nothing more than HTML file. To create a view using CLI tool , hit below command in your console.

$ > php console make:view HelloWorldView

A view class template looks like this.

<?php 
/**
 * Class : HelloWorldView
 * render data with template and output html content
 */

class HelloWorldView extends View{

 	public $template_name = 'helloWorld.blade' ;
 	public $data ;

 	public function __construct(){
 		// intilizing HelloWorldModel 
 		$this->model = new HelloWorldModel ; 		
 	}

 	public function output(){
 		$this->data = $this->model->get_test_data() ;
 		$this->render($this->template_name , $this->data) ;
 	}

}

?>

In $template_name we can mention the template which we want to use for the particular view class. You have to create a template name helloWorld.blade.html in a /templates directory in your root folder .

Creating a model

A model is script which contains the logic to interact with the database table. A single model is associate with a single table in a database.Model uses the data abstraction layer to interact with the database. To create a model in leggo using as CLI tool, you just need to hit below command.

$ > php console make:model HelloWorldModel  -t helloWorldTable

A typical a model look like this

<?php 
/**
* Class : HelloWorldModel 
* This class fetch data from the HelloWorldTable from database
*/

class HelloWorldModel extends Model{
   private $table_name ='HelloWorldTable' ;

   public function get_test_data(){
   	// intilizing model instance 
   	parent::__construct() ;
   
   	$sql = 'SELECT * FROM '.$this->table_name ;
   
   	$this->db->query($sql) ;
   	$row = $this->db->resultset() ;
   
   	return $row ;
   }
}

?>

A HelloWorldModel inhert functionalities from the base model model.php . Here model.php look like :

<?php 
/**
 * Class :Model 
 * 
 * Create a new instance of the Database class.
 * 
 * The Model class is an abstract class that creates
 * a new instance of the Database class, allowing us
 * to interact with the database without having to create
 * a new instance in each class.
 */
require $_SERVER['DOCUMENT_ROOT'].'/live tracking/vendor/database/Database.php'  ;

use database\Database as Database ;

abstract class Model{

	public $db ;

	public function __construct(){
		/**
		 * It creat a database handler oject using it child models can interact with database layer and db
		 */
		$this->db = new Database() ;
		
	}
}

?>

[2] Database:Migrations

2.1 What is Migrations ?

Migrations are like version control for your database, allowing your team to easily modify and share the application's database schema. Migrations are typically paired with leggo’s schema builder to easily build your application's database schema. If you have ever had to tell a teammate to manually add a column to their local database schema, you've faced the problem that database migrations solve.

2.2 Generating a migration

To create a migration, use the make:migration

$ > php console make:migration createTagTable

the new migration will be placed in your vendor/migrations directory. Each migration file name contains a timestamp which allows leggo to determine the order of the migrations. The --table options may also be used to indicate the name of the table and whether the migration will be creating a new table. These options pre-fill the generated migration stub file with the specified table.

2.3 Migration Structure

A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method. Within both of these methods you may use the leggo schema builder to expressively create and modify tables. To learn about all of the methods available on the Schema builder.

For example this migration creates a tag table .
creatTagTable.php

<?php 
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

$path = str_replace("\\", "/" , $baseDir) ;
$path .='/vendor/database/Migration.php' ;

require $path ;

use database\migration\Migration as Migration ;

//migation template
class CreateTagTable extends Migration{
	public $table_name = 'tagsTest' ;

	public function up(){
		#code goes here...
		$this->create($this->table_name , '[
			{
				"columnName":"id" ,
				"attributes":["INT(6)" ,"NOT NULL" ,"AUTO_INCREMENT" , "PRIMARY KEY"] 
			} ,

			{
				"columnName":"tagName" , 
				"attributes" :["VARCHAR(20)" ,"NOT NULL"]
			} ,
			{
				"columnName":"testField" ,
				"attributes":["VARCHAR(50)" ,"NOT NULL" ]
			}
		]');
	}

	public function down(){
		#code goes here...
	}
}

?>

In up() function we defined the structure of table in JSON format . When you run this migration you will find that a table name 'tagTest' create by leggo in our database .

Each child migration inherit its properties and functionalites from its base migration class . you can fin base migration file migration.php in /vendor/database/ directory . Its look like this .
migration.php

<?php 
namespace database\migration;
/**
 * Class : Migration
 * This class provide the functionailty to create tables
 */

# Including database drivers 
#$path = __DIR__.'\Database.php' ;
#require $path ;

#use database\Database as DB;

class Migration{

	/**
	 * This function lets you create table 
	 */
	protected function create($tableName , $params){
		
		#DB::test() ;
		#var_dump( json_decode($params) );
		$feed = json_decode($params) ;
		$sql = 'CREATE TABLE '.$tableName.'(' ;

		# parsing json feed 
		foreach($feed as $obj=>$k){
			$attr = implode(' ', $k->attributes ) ;
			$sql .= ' '.$k->columnName.' '.$attr.',' ;

		}
		$sql = substr($sql , 0, -1) ;
		$sql .=')' ;

		#connecting to database 
		$conn = new \mysqli("localhost", "root", "" ,"jaipur_transit");

		if ($conn->connect_error) {
    		die("Connection failed: " . $conn->connect_error);
		} 
		
		if ($conn->query($sql) === TRUE) {
    		echo "Table $tableName created successfully";
		} else {
    		throw new \Exception("oops something happened check your schema again...") ;
		}

		$conn->close();

	}

	/**
	 * This function build the sql query 
	 */
	public function schema(){
	}

	
}
?>

2.4 Running a migration

To run all of your outstanding migrations, execute the migrate php console command.

$ > php console migrate createTagTable

[3] Datbase:Seeders

3.1 What is Seeders ?

Leggo includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the Vendor/seeder directory. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order. Seeder class extends Database class .It also uses a third party plugin named faker to generate fake data and insert automatically into the database without need to expilicitly inset the rows of data.

3.2 Writing a seeder

To generate a seeder, execute the make:seeder php console command. All seeders generated by the framework will be placed in the vendor/seeder directory

$ > php console make:seeder userTableSeeder

Leggo seeders uses a package called faker to insert a fake data . Instering a fake data automatically will comes handy while developing and testing a application .It eliminates the need to manually insert rows of data into database .

We can define a fake data in a seed file like this.

<?php 

/**
 * including Seeder.php
 */
$vendorDir = dirname(dirname(__FILE__));

$path = str_replace("\\", "/" , $vendorDir) ;
echo 'path='.$path ;

if(file_exists( $path.'/database/Seeder.php' )){
	echo 'file included successfully...' ;

	require $path.'/database/Seeder.php' ;
}else{
	echo 'file not exits...' ;
}

use database\seeder\Seeder as Seeder ;

class UserSeeder extends Seeder{

	public function factory(){
		
		/**
		 * Define your faker data structure 
		 * column name followed by the type of data you want in it 
		 */
		$this->define('test99' , '2' , function(){
			$faker = $this->faker ;
			return [
				'name' =>$faker->name ,
				'email'=>$faker->email 
				
				] ;
		}) ;
	}

	public function test1(){
		echo 'hello test test' ;
	}
}

$obj = new UserSeeder ;
$obj->factory() ;
?>

[4] Framework Dependencies

Dependencies : Dependencies are the third party packages or code that is integrated with the framework to extent either the functionality of a framework or to support the inner functionality. Here is the dependencies for the leggo framework are listed :

  • Fznaninotto/Faker: php liberary to generate fake data
  • Symphony console component: working with CLI console.
  • Pusher : push notification liberary.
  • Data abtractionlayer: A wrapper for interacting with database.