安装 / leggo
基于MVC的框架,适用于Web艺术家
Requires
- php: >=5.3.0
- another-vendor/package: 1.*
This package is not auto-updated.
Last update: 2024-09-19 17:18:59 UTC
README
基于PHP编写的MVC框架
目录表
- 关于Leggo
- 安装Leggo
- Leggo框架架构
- Leggo的工作原理
- Leggo目录层次结构
- 定义你的Web路由
- 为你Web路由创建控制器
- 创建视图
- 创建模型
- 数据库:迁移
- 什么是数据库迁移
- 生成迁移
- 迁移结构
- 运行迁移
- 数据库:种子文件
- 什么是种子文件
- 编写种子文件
- 运行种子文件
- 框架依赖项
关于Leggo
Leggo是一个轻量级的基于MVC的框架,用PHP编写。它提供了一个CLI客户端来促进快速开发。它适用于想要自定义代码而不必担心底层的开发者。Leggo提供了一个简单的目录层次结构,您可以轻松地与之工作。与Laravel、symphony、cakePHP等其他重量级框架不同,单个脚本中不包含很多文件。开发者可以轻松地确定当前脚本/代码使用了哪个框架文件。这种主要优势是自定义。现在,当开发者知道背景中发生了什么时,他们可以很容易地根据他们自己自定义代码。默认情况下,提供了一个数据抽象层来与数据库交互并动态处理所有SQL事务。如果开发者想要集成ORM,他们可以选择集成他们自己的ORM。Leggo使用模板的概念,我们可以在模板目录中为特定路由定义模板。要访问模板,我们只需提供带有模板名称的 $template_name 变量。目前,它没有使用任何模板引擎。但在Leggo的后续版本中,开发者可以轻松地集成他们选择的模板引擎。Leggo是一个纯MVC框架,其中所有数据都由其视图处理。它提供了开发强大、可扩展且安全的Web应用程序的能力,而不必烦恼于逻辑部分。它便于用户集成ORM(对象关系映射)如Propel、Doctrine等,以及他们选择的模板引擎。它不会强迫用户使用任何特定的
安装Leggo
您可以通过两种方式将Leggo下载到您的机器上
从GitHub安装
要安装,请在您的git bash shell中运行以下命令
$ > git clone https://github.com/thecodestuff/leggo.git
使用包管理器安装
要使用composer安装,请运行以下命令
$ > require install/leggo
Leggo框架架构
此处插入artictech图像
Leggo在底层使用MVC设计模式。MVC代表模型-视图-控制器。在典型的MVC框架中,所有逻辑都由控制器处理,数据库交互由相应的模型处理,所有前端代码都存储在其相应的模板中,由相应的视图检索。
Leggo的工作原理
Leggo的所有内容都从定义一个Web路由开始,在vendor/app/route/web.php文件中。我们的.htaccess配置得如此之好,以至于来自客户端的每个请求都会被路由到根目录下的index.php文件。索引文件调用其中所有Web路由都编写的地方。框架会检查Web路由/URL是否有效。如果请求无效,则向用户显示错误消息;否则,Leggo将调用控制器。一旦控制器被调用,其视图和模型也会被调用。视图检查在leggo/template目录中视图类中的$template_name变量中提到的模板。
Leggo目录层次结构
定义你的Web路由
Web是一个URL,它调用特定的控制器。要定义Web路由,请导航到/vendor/route/web.php文件
Route::get('helloworld/' , 'HelloWorldController@index') ;
Route::get函数接受两个参数。第一个参数是你的路由名称,第二个参数是控制器名称和函数名称。
为你Web路由创建控制器
Leggo附带其CLI工具,使用该工具你可以为你的路由创建控制器文件。要创建控制器文件,请使用以下命令。
$ > php console make::controller HelloWorldController
导航到你的/vendor/app/controllers目录,你会看到你的控制器已经创建,并为你添加了一些代码,以便你可以开始。
你的典型控制器文件看起来像这样
<?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() ; } } ?>
这里HelloWorldController类继承自基控制器类。基控制器有一个名为view()的函数,它调用特定路由的视图。一个典型的控制器.php文件看起来像这样
<?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' ; } } ?>
现在你已经为你的Web路由创建了一个控制器,是时候为你的控制器创建一个视图了
创建视图
视图包含将模型从数据库中获取的数据传输到模板的逻辑。它与模板一起工作,模板不过是一些HTML文件。要使用CLI工具创建视图,请在你的控制台中输入以下命令。
$ > php console make:view HelloWorldView
视图类模板看起来像这样。
<?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) ; } } ?>
在$template_name中,我们可以指定我们想要用于特定视图类的模板。您需要在您的根目录中的/templates目录中创建一个名为helloWorld.blade.html的模板。
创建模型
模型是一个包含与数据库表交互逻辑的脚本。单个模型与数据库中的一个表关联。模型使用数据抽象层与数据库交互。要使用Leggo CLI工具创建模型,只需输入以下命令。
$ > php console make:model HelloWorldModel -t helloWorldTable
典型的模型看起来像这样
<?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 ; } } ?>
HelloWorldModel从基模型model.php继承了功能。这里的model.php看起来像
<?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] 数据库:迁移
2.1 什么是迁移?
迁移类似于数据库的版本控制,允许团队轻松修改和共享应用程序的数据库架构。迁移通常与Leggo的schema builder配对,以轻松构建应用程序的数据库架构。如果你曾经告诉队友手动添加列到他们的本地数据库架构,你就遇到了数据库迁移解决的问题。
2.2 生成迁移
要创建迁移,请使用make:migration。
$ > php console make:migration createTagTable
新的迁移将放置在你的vendor/migrations目录中。每个迁移文件名都包含一个时间戳,这允许Leggo确定迁移的顺序。还可以使用--table选项来指示表名以及迁移是否会创建新表。这些选项会在生成的迁移存根文件中预先填充指定的表。
2.3 迁移结构
迁移类包含两个方法:up和down。up方法用于向数据库添加新表、列或索引,而down方法应撤销up方法执行的操作。在这两个方法中,您可以使用Leggo schema builder明确地创建和修改表。要了解所有可用的Schema builder方法。
例如,此迁移创建了一个标签表。
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... } } ?>
在上面的 up() 函数中,我们以 JSON 格式定义了表的架构。当你运行此迁移时,你将在我们的数据库中找到一个名为 'tagTest' 的表,这是由 Leggo 创建的。
每个子迁移都继承其基础迁移类的属性和功能。你可以在 /vendor/database/ 目录下找到基础迁移文件 migration.php。它看起来像这样。
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 运行迁移
要运行所有挂起的迁移,请执行 migrate php 控制台命令。
$ > php console migrate createTagTable
[3] 数据库:种子
3.1 什么是种子?
Leggo 包含了一种简单的方法,使用种子类将测试数据填充到数据库中。所有种子类都存储在 Vendor/seeder 目录下。种子类可以取任何你希望的名字,但最好遵循一些合理的命名规范,例如 UsersTableSeeder 等。默认情况下,已经为你定义了一个 DatabaseSeeder 类。从该类中,你可以使用 call 方法来运行其他种子类,从而控制种子的顺序。种子类扩展了 Database 类。它还使用一个名为 faker 的第三方插件来生成假数据,并自动将其插入数据库,无需显式插入数据行。
3.2 编写种子
要生成种子,请执行 make:seeder php 控制台命令。框架生成的所有种子都将放置在 vendor/seeder 目录 下。
$ > php console make:seeder userTableSeeder
Leggo 种子使用一个名为 faker 的包来插入假数据。在开发和测试应用程序时,自动插入假数据将非常有用。它消除了手动将数据行插入数据库的需求。
我们可以在种子文件中定义假数据,如下所示。
<?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] 框架依赖
依赖项:依赖项是与框架集成以扩展框架功能或支持内部功能的第三方包或代码。以下是 Leggo 框架的依赖项列表
- Fzaninotto/Faker:用于生成假数据的 PHP 库
- Symphony console component:用于与 CLI 控制台交互
- Pusher :推送通知库
- 数据抽象层:用于与数据库交互的包装器