对于小型和简单的项目,包括:
Sekeleton MVC建议使用MVC架构。 MVC代表模型视图控制器。模型视图控制器-Wikipedia
Model:
The central component of the pattern. It is the application's dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application.
View:
Any representation of information such as a chart, diagram or table. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants.
Controller:
Accepts input and converts it to commands for the model or view.
example.com/shop/show-shoes/index.php文件index.php文件需要自动加载(作曲家)和全局助手(稍后再详细介绍)。index.php sendresponse()中,在$ app实例上调用,该实例将适当的标题和状态代码以及发送回浏览器发送的内容。现在,您了解了框架如何处理这一切。以下是每个班级的文档,也是一些示例,可以使您开始开发。
YourAppApp :您的代码Controllers :所有控制器Model :所有型号View :所有视图Config.php :用户配置类Core :框架的代码public :接触到网络index.php :应用程序的起点.htaccess :重新安排所有请求index.phpassets :CSS/JS等所有静态资产住在这里storage :编译的视图,上传文档等。vendor :作曲家下载所需库Altough大多数框架都遵循DB参数和视图路径等配置对对流的对流。
用户配置生活在您的应用程序中的应用程序目录中。打开此文件,您可以看到Config类扩展了Baseconfig( Core Config),该文件是在骨架中内部定义的。最重要的是const parmas,一些可能是静态方法。
该类是通过注释记录的,因此请检查一下,您将可以自己弄清楚所有配置。
用于路由约定,用于ex:。
想象想显示3个ID路线的帖子将看起来像这样
example.com/user-post/show-photos/3/paris
上面的示例有
因此,上面的控制器类看起来像
<?php
namespace App Controllers ;
use Core Request ;
class UserPost {
public function showPhotos ( Request $ request , $ id , $ location ) {
# $id= 3 and $location= paris
return view ( ' photos.show ' , [
' id ' => $ id ,
' location ' => ' paris '
]);
}
}但是您可能会问 /, /关于等等的路线?
这些路由会自动路由到default_controller(可以在配置中更改)默认值为sitecontroller, / / action默认为索引方法,该方法也可以在配置中更改。
您还可以注意始终将CoreRequest对象传递到第一个参数。它包含各种请求的帮助者(请求部分中的更多信息)。
请求被视为对象。它将通过聋人传递给每个控制器的方法,因为第一个参数将以序列后传递。前任:
对于URL example.com/user-post/show-photos/3/paris
<?php
namespace App Controllers ;
use Core Request ;
class UserPost {
public function showPhotos ( Request $ request , $ id , $ location ) {
# $id= 3 and $location= paris
return view ( ' photos.show ' , [
' id ' => $ id ,
' location ' => ' paris '
]);
}
}这是请求对象提供的一些方便方法。
/* Check Method of request */
$ request -> isGet ();
$ request -> isPost ();
/*
How to get superglobals
These methods if provided returns specific field or entire array if no parameters are passed.
If field does not exits null is returned
*/
$ request -> query ( ' field ' ); // For GET
$ request -> input ( ' field ' ); // For POST
$ request -> server ( ' field ' ); // For SERVER
$ request -> files ( ' field ' ); // For FILES
$ request -> all (); // For FILES and POST merged 需要将需要发送到浏览器的响应可以通过
view ( ' viewpath ' , $ data = []); // viewpath ex. phtots.show for Views/photos/show.php
view ([ ' header ' , ' content1 ' , ' content2 ' , ' footer ' ], $ data = []); // array alternative
json ( $ data = [], $ options , $ depth = 512 ); // Same as setting header("Contetnt-Type: text/json"); and using json_encode() 验证的灵感来自Laravel,很少提供基本和大多数使用的规则。
$ errorMsgs = Core Validator:: validate ( $ request -> all (), [
" name " => [ " required " , " max:30 " ],
" mobile " => [ " required " , " digits:10 " ],
" photo " => [ " required " , " image:image/jpeg " , " max:100 " ],
]);框架生成的消息很简单,您可以自定义其中的配置文件。前任。
public static function getMessage ( $ rule , $ field , $ params , $ messages = [])
{
$ messages = [
" required " => " Hmm... plz get the $ field filled :) "
];
return parent :: getMessage ( $ rule , $ field , $ params , $ messages );
}在上面的示例中,所需消息将是自定义的。
如果根据PHP的空()函数将字段留为空,则会失败。
必须是字符串值。
必须是数字或数字字符串值,如果没有给定尺寸,则简单地检查为数字值
是的,是,是,否,是,否,true,false,0,1否则为false
请求数据必须包含带有_Confirmation后缀的同一名称的字段。例如:如果在密码上使用,它将检查password_confircation字段
是否是有效的电子邮件ID。
检查是否有效上传文件
检查是否有效图像。默认情况下允许的类型:Image/jpeg,Image/gif,Image/png,Image/WebP,Image/svg+XML,Image/BMP。
是否通过strtotime()函数传递的有效日期
检查两个日期是否相等
检查用户日期是否在给定的日期之后。
检查用户日期是否在给定的日期之前。
验证和给定的字段必须具有不同的值。
验证和给定的字段必须具有相同的值。
必须存在验证的字段可能为空或空。
最大尺寸规则。为了
数字:数字必须小于或等于大小。
字符串:长度必须小于或等于大小。
数组:元素计数必须小于或等于大小。
上载文件:文件大小必须小于或等于大小。
最小规则。约束与最大规则相同。
在配置文件中设置数据库凭据和DB类型。然后,您可以随时致电以下连接:
$ conn = Core Database:: getConnection (); // PDO instance 模型是您的企业所在的地方,其中确实包括与DB相关的逻辑。这就是为什么提供很少有方便的方法的原因:
// suppose
class User extends Model { /* Empty */ }
$ user = new App Models User ();
$ user -> find ( $ id , columns = [ ' * ' ], $ fetchStyle = PDO :: FETCH_BOTH ); // Get single row
$ user -> all (columns = [ ' * ' ], $ fetchStyle = PDO :: FETCH_BOTH ); // get all rows
$ user -> insert ( $ data ); // As associative array of column name and values
$ user -> update ( $ id , $ data ); // $id to be updated with $data
$ user -> delete ( $ id ); // Row to delete with $id模型将表明表的名称为您的班级名称的蛇case复数形式。因此,在上面的情况下,用户类将是用户相同的AppData是App_Data,而画廊将是画廊。您可以通过覆盖$table属性来定义自己的名称,相同的方式$id被视为默认的主键,并且也可以被覆盖。
class User extends Model
{
protected $ table = " my_table " ;
protected $ id = " tbl_id " ;
}任何复杂的事情都需要自己查询。前任:
use Core Model ;
use Core Database ;
class User extends Model
{
public function deleteByName ( $ name )
{
$ sql = " DELETE FROM $ this -> table WHERE name=? " ;
return Database:: getConnection ()
-> prepare ( $ sql )
-> execute ([ $ name ]);
}
}为了获取最后一个插入ID,您可以执行Database::getConnection()->lastInsertId() 。