andikaryanto / nayoframework
nayo 框架是一个 MVC php 框架
Requires
- php: ^7.2
- andikaryanto11/route: ^0.0.1
- clxcommunications/sdk-xms: ^1.1
- eftec/bladeone: ^3.30
- firebase/php-jwt: ^5.0
- icanboogie/inflector: ^1.4
- madskullcreations/simplicity: dev-master@dev
- nexmo/client: ^1.8
- php-curl-class/php-curl-class: ^8.6
- szenis/routing: ^2.0
- web-token/jwt-framework: ^1.3
- yidas/codeigniter-rest: ^1.7
- yidas/phpspreadsheet-helper: ^1.3
README
Nayo 框架是 php MVC 基础,由 Code Igniter 和 Entity 框架 (.Net EDMX) 启发构建
主要目的:我创建这个应用程序是为了分享 MVC 代码,如果有人想了解 php MVC 如何工作。代码很简单,可以被每个人修改。
安装
- 为 php 安装 composer
- 将 app 复制到您的 Web 服务器上
- 从 cmd/console 进入 Framework 文件夹。输入 "composer update"
简单指南
这是一个受 CodeIgniter 启发的应用程序。在配置(App\Config)方面,大多数看起来像 CodeIgniter
与 CodeIgniter 的主要区别在于模型(ORM 类似)和迁移,因此我提供了 Modeling 的简单解释
0. 配置
App\Config\Databse.php 支持 2 个数据库驱动 mysql 和 sqlsrv php(用于 sql server > 2008,例如 2012 或更高版本)请参阅 php 设置以 ODBC 驱动
'connectionstring' => "",
'host' => '', // host of database ex : 192.168.1.xxx or .\MSSQLSERVER
'user' => '',
'password' => '',
'dbname' => '',
'driver' => '.mysqli', //.mysqli, .sqlsrv
'port' => '',
'charset' => '',
'engine' => ''
1. 模型
模型类必须扩展 Nayo_Model 类
- 表名必须是复数形式
- 文件名必须与表名相同,例如:Examples.php,首字母大写
- 类名必须与表名相同,例如:Examples,首字母大写
- 表必须有一个字段 Id(int,自动递增)
- 我在 App\Database\Migrations 中提供了一些查询表创建示例
以下是一个示例
<?php
namespace App\Models;
use Core\Nayo_Model;
class Examples extends Nayo_Model {
/**
* define all your fields table here
*/
public $Id;
public $Name;
/**
* $table set as name of table
* $entity set as name of table with first upper case letter
*
* if your table named like m_example
* it would be
* $table = m_example
*/
/**
* it's ORM look like base
* so you have to name your table as example
*
* case : if you have 2 tables related it should be like this
* table 1 : m_groupusers(Id, name, ....)
* table 2 : m_users(Id, M_Groupuser_Id,....)
* foreign key should be Example_Id , ex : M_Groupuser_Id
*/
protected $table = 'examples';
}
方法
如果模型类扩展 Nayo_Model 类,则该类将具有 CRUD 方法
- modelclass->query($sql);
- modelclass::get($id)
示例
Example::get(1);
//this method wiil return result table with id = 1
// Example is the object class that is mapped to table;
- modelclass->save()
如果对象 Id(字段)已设置,例如:Id = 1,此方法将更新数据,否则将保存新数据示例
//save data
$model = new Example();
$model->Name = 'new Name';
$model->save();
//update data
$model = Example::get(1);
$model->Name = 'edited Name';
$model->save();
- modelclass->delete()
示例
$model = Example::get(1);
$model->delete();
//this method will delete data with Id = 1
-
modelclass::getAll($array) $array 是可选的示例
$params = array( 'where' => array( 'Name' => 'existname' ) ); //filtered $result = Example::getAll($params); //or get all data $result = Example::getAll();
-
modelclass->get_Entity() Entity 是与对象表相关联的名称,首字母大写
此方法将获取与多对一关系相关联的数据
示例
//Tests is a model class object
foreach(Tests::getAll() as $test){
echo $test->get_Example()->Name; // get_EntityName() get related table data
}
- modelclass->get_list_Entity() Entity 是与对象表相关联的名称,首字母大写方法将获取与一对多关系相关联的数据
示例
$data = Example::get(1);
foreach($data->get_list_Test() as $test){
}
2. 控制器
控制器类必须扩展 Nayo_Controller 类
以下是一个示例
<?php
namespace App\Controllers;
use Core\Nayo_Controller;
use App\Models\Tests;
class Example extends Nayo_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->view('example/index');
}
public function load_data(){
$data['model'];
$this->view('example/index', $data);
}
/**
* using model
*
* you can read all function in Core\Model
*/
public function test(){
foreach(Tests::findAll() as $test){
echo $test->get_Example()->Name; // get_EntityName() get related table data
}
}
}
视图
您可以看到控制器,如何加载视图和传递数据
路由
有关更多信息,请参阅 Nezamy 路由:https://nezamy.com/Route/
CLI
CLI 用于生成 CONTROLLERS、MODELS、MIGRATION 并迁移所有迁移
帮助
用于查看可用的命令行选项
php index.php help
控制器
将在 App\Controllers 中生成控制器文件
php index.php controller controller_name
模型
将在 App\Models 中生成模型文件,它必须具有与表名相关的参数,区分大小写,并且将包含您表中的所有属性
php index.php model table_name
迁移
将在 App\Database\Migrations 中生成迁移文件,文件名以当前时间为名,例如:Migration_yyyymmddhhiiss.php
php index.php migration
<?php
namespace App\Database\Migrations;
use Core\Database\Table;
class migration_20190620045801 {
public function up(){
//creating table
$table = new Table();
$table->table('m_companies');
$table->addColumn("Id", "int", "11", false, null, true, true);
$table->addColumn("CompanyName", "Varchar", "50");
$table->addColumn("Address", "Varchar", "300");
$table->addColumn("PostCode", "Varchar", "10");
$table->addColumn("Email", "Varchar", "300");
$table->addColumn("Phone", "Varchar", "50");
$table->addColumn("Fax", "Varchar", "50", true);
$table->addColumn("UrlPhoto", "Varchar", "500", true);
$table->addColumn("CreatedBy", "Varchar", "50", true);
$table->addColumn("ModifiedBy", "Varchar", "50", true);
$table->addColumn("Created", "datetime", "", true);
$table->addColumn("Modified", "datetime", "", true);
$table->create();
}
}
迁移
将迁移所有尚未在数据库迁移中进行的迁移
php index.php migrate
数据表
服务器端处理数据表将返回 json 格式到
<?php
namespace App\Controllers;
use App\Models\Example;
use Core\Libraries\Datatables;
class M_foodcategory extends Base_Controller{
public function index(){
$params = [
'where' => [
'Name !' => 'a'
]
];
$datatable = new Datatables('M_Examples', $params); // $params are optional
$datatable
->addColumn(
'Id',
function($row){
return $row->Id;
},
false,
false
)->addColumn(
'Name',
function($row){
return "<td>".
formLink($row->Name, array("id" => $row->Id,
"href" => baseUrl('mfoodcategory/edit/'.$row->Id),
"class" => "text-muted"))
."</td>";
}
)->addColumn(
'Description',
function($row){
return $row->Description;
}
)->addColumn(
'Created',
function($row){
return $row->Created;
},
false
)->addColumn(
'Action',
function($row){
return "<td class = 'td-actions text-right'>".
formLink("<i class='fa fa-trash'>", array(
"href" => "#",
"class" => "btn-just-icon link-action delete",
"rel" => "tooltip",
"title" => lang('Form.delete')))
."</td>";
},
false,
false
);
echo json_encode($datatable->populate());
}
}