使用PHP构建简单网站的最快方法!
安装
关于Velox
建筑学
config
课程
功能
命令
主题
扩展Velox
MVC
模板
验证
ChangElog
文档
如果您喜欢这个项目并希望支持其开发,那么将不胜感激!

查看演示或与REPL一起玩。
你想读书吗?在marwanalsoltany.github.io/velox上的文档网站上查看完整的API。
composer create-project marwanalsoltany/velox my-velox-app注意:您可能需要根据版本/分支添加--stability=dev 。您可能还需要添加--no-dev标志以不安装开发依赖性。
git clone https://github.com/MarwanAlsoltany/velox.git my-velox-app事实:如果您不想使用其他任何第三方软件包。使用git安装Velox就足够了。
将Velox作为.zip或.tar.gz下载,然后在服务器Web根目录中提取它。
建议:如果您想快速测试Velox,并且没有任何可用的Web服务器,请使用任何安装方法,并从内部Velox目录运行php bin/app-serve 。此命令将在localhost:8000 (请注意,您需要在系统上至少安装PHP)。
Velox是一款轻巧的微型框架,它使使用PHP Joy Full创建一个简单的网站。它可以帮助您更快,更有效地创建防止未来的网站。它提供了促进使用PHP创建网站的过程。 Velox没有任何依赖项,Velox软件包及其所需的所有内容都包含在项目本身中。 Velox提供的只是一种使用配置,传递数据,注册路由,与数据库交互,渲染视图,处理异常,自动加载代码和解决资产的方法。它提供了MVC设计模式的视图和控制器部分。 Velox从v1.3.0盯着V1.3.0,还提供了模型部分,使其成为MVC框架的完整特色,从v1.4.0开始,它还带有简单的身份验证系统。如果您最需要的是HTML文件,Velox也可以用作静态站点生成器。
Velox的创建是为了解决一个特定的问题,它是一种在动态和静态之间建立网站的方法,一种创建一个简单网站的网站,而不会被迫使用框架或CMS,或者带有一吨永远不会使用的东西的CMS,它的轻量级,最小,最小和直接。
如果您有一个中介项目,则不建议使用Velox,最好使用一个完善的框架。 Velox不是重塑车轮的主动性,您可以将Velox视为小型项目的起动器。
Velox具有非常特殊的用例,简单的网站,这是非常简单的网站。优势是,您没有不需要的东西。将Velox与Laravel或Symfony进行比较是无关紧要的,因为这些框架在完全不同的区域中发挥了作用,还值得一提的是,Velox比Lumen或Slim更简单。
| 目录 | 描述 |
|---|---|
bootstrap | 这是Velox引导程序的地方。通常,您不必更改此目录中的任何内容,除非您想将Velox功能扩展到基本内容之外。 |
bin | 这是放置PHP可执行文件的地方。您可以自由添加您的内容,也可以删除整个目录。 |
app | 这是您自己的后端逻辑的放置位置。您将主要在这里为应用程序的后端部分工作。 |
classes | 这是Velox源文件的现场。除非您想制作自己的Velox版本,否则您不应该在这里触摸任何东西。 |
functions | 这是应用程序中加载的所有功能实时的功能。您可以自由添加您的内容,也可以删除整个目录。 |
includes | 这是所有应该预加载的文件的地方。您可以自由添加您的内容,也可以删除整个目录。 |
themes | 这是您所有前端主题都将被放置的地方。您将主要在这里为应用程序的前端部分工作。 |
config | 这是所有配置文件都可以使用的地方。在运行时使用Config类,此处的所有文件都可以访问。 |
storage | 这是Velox将编写缓存和日志的地方。您也可以使用此目录来存储整个安装范围的资产。 |
public | 在这里,您应该将index.php放置在静态资产(例如,主题assets/目录)的符号链接中,以获得最大的安全性。如果需要,您可以自由删除此目录。 |
vendor | 这是您的作曲家依赖项的放置位置。如果您不想使用作曲家,则可以自由删除此目录。 |
建议:记录了这些目录中列出的大多数文件。看看他们,以了解有关Velox的更多信息。
Velox应用程序的输入点是index.php ,在这里您需要bootstrap/autoload.php ,使用Router::class在其处理程序中注册一些路由,然后启动路由器。这就是您拥有一个工作的Velox应用所需的一切。
<?php
require ' bootstrap/autoload.php ' ;
Router:: handle ( ' / ' , function () {
return View:: render ( ' home ' , [ ' title ' => ' Home ' ]);
});
Router:: start ();此外,您可以使用Router::middleware()和/或使用{global.errorPages.CODE} config value添加404 () 405 /或设置处理程序。
另外,您可以在其自己的文件中提取“路由注册部分” ,并使用bootstrap/additional.php让Velox知道它。从v1.2.0开始,默认情况下,文件includes/routes/web.php是您应该注册路由的地方。如果未明确启动,路由器也将自动启动。
注意:为了使Velox正确,可以安全地工作,您需要将所有请求重定向到应用程序输入点( index.php ),并将所有请求阻止到服务器上的其他PHP文件(请查看.htaccess.dist ,以开始使用Apache)。
下表列出了用Velox发货的所有配置文件。
| 配置文件 | 描述 |
|---|---|
global.php | 此配置文件包含一些几乎所有类(App Wide Config)使用的一些全局变量。 |
router.php | 此配置文件可用于覆盖Router::class默认参数。 |
session.php | 此配置文件包含会话配置, Session::class使用。 |
database.php | 此配置文件包含数据库凭据,该数据库由Database::class使用。 |
auth.php | 此配置文件包含身份验证配置,由Auth::class使用。 |
theme.php | 此配置文件可用于编辑/扩展主题配置。 |
view.php | 此配置文件可用于自定义有关视图的所有内容。 View::class使用。 |
data.php | 此配置文件可用于提供任何任意数据,然后将其注入Data::class 。 |
cli.php | 此配置文件可用于启用/禁用命令或更改其参数。 |
事实:您也可以自由添加自己的配置文件,您需要做的就是在/config下创建一个新文件,然后将配置添加到它。 Velox将知道此文件并将其加载到应用程序中。您可以通过Config::get('filename.whateverKeyYouWrote') 。
Velox类分为四个名称空间:
MAKSVeloxMAKSVeloxBackendMAKSVeloxFrontendMAKSVeloxHelper下表列出了所有可用的类别的说明:
| 班级 | 描述 |
|---|---|
VeloxApp | 作为Velox的基本服务容器的课程。 |
BackendEvent | 提供简单的事件处理功能(调度和侦听)的课程。 |
BackendConfig | 一个从/config Directory加载所有内容的类,并将其作为数组,可通过点通知访问。 |
BackendRouter | 作为路由器和应用程序的切入点的类。 |
BackendGlobals | 可以用作抽象/包装的课程,可与超级全局合作。 |
BackendSession | 一个提供简单接口的课程,可以与会话一起使用。 |
BackendController | 一个抽象类,可作为基本控制器,可以扩展以为路由器制造处理程序。 |
BackendDatabase | 代表数据库并处理数据库操作的类。 |
BackendModel | 一个抽象类,可作为基本模型,可以扩展以创建自定义模型。 |
BackendAuth | 作为用户身份验证系统的类。 |
FrontendData | 可以通过点通知可以访问的抽象数据袋/存储的类。 |
FrontendView | 一个类,可以将文件(布局,页面和部分)视为包含其他文件,将页面内容分为部分和缓存呈现视图的能力。 |
FrontendHTML | 作为流利界面的类,以PHP编写HTML。它还有助于即时创建HTML元素。 |
FrontendPath | 用于应用程序的不同路径/URL的路径解析器的类。 |
HelperDumper | 一个类,以不错的格式转储变量和异常。 |
HelperMisc | 作为各种其他效用函数的持有人的类。 |
注意:这是Velox软件包提供的所有内容。
事实: App , Event , Config , Router , Globals , Session , Database , Auth , Data , View , HTML , Path类在root名称空间上被混合使用,以易于使用。
Velox功能分为这些文件:
helpers.php :这是Velox类实时助手功能的地方,这些主要是返回类或别名的实例的函数。html.php :这是HTML Helper函数活的地方,这些功能无非是使用HTML的最常用的PHP功能的别名。下表列出了所有可用功能以及它们所属的类/组:
| 班级/组 | 功能 |
|---|---|
App::class | app() ,abort() ,terminate() |
Event::class | event() |
Config::class | config() |
Router::class | router() ,handle() ,redirect() ,forward() |
Database::class | database() |
Globals::class | globals() |
Session::class | session() ,flash() ,csrf() |
Auth::class | auth() |
View::class | view() ,render() ,render_layout() ,render_page() ,render_partial() ,section_push() ,section_reset() ,section_start() ,section_end() ,section_yield() ,include_file() |
Data::class | data() ,data_has() ,data_get() ,data_set() |
HTML::class | html() |
Path::class | path() ,app_path_current() ,app_url_current() ,app_path() ,app_url() ,theme_path() ,theme_url() ,assets_path() ,assets_url() |
Dumper::class | dd() ,dump() ,dump_exception() |
| HTML帮助者 | he() , hd() , hse() , hsd() , st() , nb() |
事实:您也可以自由地添加自己的功能,您需要做的就是在/functions下创建一个新文件并将您的功能添加到其中。 Velox将知道此文件并将其加载到应用程序中。
Velox带有一些方便的命令,您可以使用这些命令来执行一些重复的任务。您可以使用php bin/{command-name}执行这些命令。
下表列出了所有可用的命令,其中包括其描述。
| 命令 | 描述 |
|---|---|
app-serve | 此命令启动开发服务器。 |
app-mirror | 该命令反映了/public目录中的应用程序。 |
config-cache | 此命令缓存当前配置。 |
config-dump | 此命令用语法突出显示了当前配置。 |
cache-clear | 此命令清除了缓存。 |
您可以使用config/cli.php文件自定义这些命令。在这里,您可以启用/禁用它们或为它们提供不同的论点。
如果您想使所有这些命令都可以通过单个接口访问。查看我的其他软件包混合物,这将为您乃至更多。
Velox围绕主题的想法建立,主题分为四个目录:
assets/目录是将所有与此主题关联的静态资产所在的地方。layouts/目录是您定义布局的地方。 Velox术语中的布局是网页的外部框架。pages/目录是您在每个页面上都放置内容的地方,然后将页面包裹在您选择的某种布局中,并最终呈现。 Velox术语中的页面是网页的实际内容。partials/ directory是您将所有可重复使用的主题片段放置在这里,然后将其用于布局,页面或其他部分。部分的一个很好的例子可能是:组件,包括和内容元素。您可以使用config/theme.php文件自定义主题的行为。在这里,您可以使用active键设置活动主题。主题可以通过将parent键设置为父键来彼此继承。如果您希望使用paths密钥,也可以更改主题目录结构。值得查看与主题有关的其他配置(例如缓存)可以在config/view.php文件中找到。
建议:您可以查看提供的velox主题,以了解所有内容如何在实践中共同工作。
themes/velox/layouts/main.phtmlthemes/velox/pages/home.phtmlthemes/velox/partials/text-image.phtml 要添加自己的课程,请使用app/目录,这是您应该使用自己的业务逻辑的地方。请注意,您必须关注PSR-4才能使Velox加载课程。请参阅app/Controller/DefaultController.php ,以获取一个想法。
这是与Velox一起工作时应考虑的一些重要文件的列表:
autoload/additional.php 。config/data.php 。includes/routes/web.php (从v1.2.0开始)。includes/events/system.php (从v1.2.0开始)。 <?php
namespace App Model ;
use MAKS Velox Backend Model ;
class Person extends Model
{
protected static ? string $ table = ' persons ' ;
protected static ? array $ columns = [ ' id ' , ' first_name ' , ' last_name ' , ' age ' , ...];
protected static ? string $ primaryKey = ' id ' ;
public static function schema (): string
{
// return SQL to create the table
}
} <?php
use App Model Person ;
// creating/manipulating models
$ person = new Person (); // set attributes later via setters or public assignment.
$ person = new Person([ ' first_name ' => $ value , . . . ]); // set attributes in constructor
$ person -> get ( ' first_name ' ); // get an attribute
$ person -> set ( ' last_name ' , $ value ); // set an attribute
$ person -> getFirstName (); // case will be changed to 'snake_case' automatically.
$ person -> setLastName ( $ value ); // case will be changed to 'snake_case' automatically.
$ person -> firstName ; // case will be changed to 'snake_case' automatically.
$ person -> lastName = $ value ; // case will be changed to 'snake_case' automatically.
$ attributes = $ person -> getAttributes (); // returns all attributes.
$ person -> save (); // persists the model in the database.
$ person -> update ([ ' first_name ' => $ value ]); // updates the model and save changes in the database.
$ person -> delete (); // deletes the model from the database.
Person:: create ( $ attributes ); // creates a new model instance, call save() on the instance to save it in the database.
Person:: destroy ( $ id ); // destroys a model and deletes it from the database.
// fetching models
$ count = Person:: count (); // returns the number of models in the database.
$ person = Person:: first ();
$ person = Person:: last ();
$ person = Person:: one ([ ' first_name ' => ' John ' ]);
$ persons = Person:: all ([ ' last_name ' => ' Doe ' ], $ order , $ offset , $ limit );
$ person = Person:: find ( $ id ); // $id is the primary key of the model.
$ persons = Person::find( ' first_name ' , ' John ' , ' last_name ' , ' Doe ' . . .); // or
$ persons = Person:: find ([ ' first_name ' => ' John ' , ' last_name ' => ' Doe ' ]);
$ persons = Person:: findByFirstName ( ' John ' ); // fetches using an attribute, case will be changed to 'snake_case' automatically.
$ persons = Person:: where ( ' first_name ' , ' = ' , $ value ); // fetches using a where clause condition.
$ persons = Person:: where ( ' last_name ' , ' LIKE ' , ' %Doe ' , [[ ' AND ' , ' age ' , ' > ' , 27 ], ...], ' age DESC ' , $ limit , $ offset );
$ persons = Person:: fetch ( ' SELECT * FROM @table WHERE `first_name` = ? ' , [ $ value ]); // fetch using raw SQL query. <?php
namespace App Controller ;
use MAKS Velox Backend Controller ;
use App Model Person ;
class PersonsController extends Controller
{
public function indexAction ()
{
$ persons = Person:: all ();
return $ this -> view -> render ( ' persons/index ' , [
' title ' => ' Persons ' ,
' persons ' => $ persons
]);
}
// other CRUD actions ...
/**
* Persons search action.
*
* @route("/persons/search", {GET})
*/
public function searchAction ()
{
// ...
}
/**
* Persons middleware.
*
* @route("/persons/*", {GET, POST})
*/
public function personsMiddleware ()
{
// ...
}
}事实: CRUD操作(即: index , create , store , show , edit , update , destroy )被注册和默认配置。要自动注册自己的路由,请使用@route("<path>", {<http-verb>, ...})注释。请参阅Controller::registerRoutes() dicblock以了解更多信息。
事实:要使模型可作为控制器的属性( $this->model )可用,请使用Controller::associateModel() 。请参阅Controller::associateModel() disblock以了解更多信息。
建议:如果在生产模式下作为快捷方式,则使用匹配的代码{global.errorPages.CODE} config value将异常呈现为相应的错误页面。例如,当抛出new Exception('Not found', 404)形式时,将渲染配置的404错误页面,并将传递异常消息。如果页面未配置,则500错误页面将呈现为后备。
Velox没有任何验证数据的方法。查看我的其他软件包,这将为您乃至更多。
{# theme/pages/persons/index.phtml #}
{! @extends 'theme/pages/persons/base' !}
{! @block content !}
{! @super !}
< h1 > {{ $title } } </ h1 >
{! @if ( isset ( $persons ) && count ( $persons ) ) !}
< ul >
{! @foreach ( $persons as $person ) !}
< li > {{ $person -> firsName } } {{ $person -> lastName } } </ li >
{! @endforeach !}
</ ul >
{! @endif !}
{! @endblock !}建议:查看Person模型和PersonsController ,以查看一个现实的例子。
Velox配备了自己的模板引擎。如果您有任何其他模板引擎的经验,那么该模板引擎非常直观且易于融合。请注意,使用此模板引擎是可选的。您只需在视图中使用原始PHP即可。
下表列出了所有可用标签及其所做的标签:
| 标签 | 描述 |
|---|---|
{! @extends 'path/to/template' !} | 扩展一个模板,该模板的块将被继承。 |
{! @include 'path/to/file' !} | 包括文件,这将在包含之前呈现(无法访问上下文变量)。 |
{! @embed 'path/to/file' !} | 嵌入文件,将包含在(可以访问上下文变量)。 |
{! @block name !}{! @endblock !} | 创建一个块来包装一些代码。 |
{! @super !} | 在扩展模板中的一个块中使用它来继承父块内容。 |
{! @block(name) !} | 打印一个块。需要至少一次调用一次才能渲染一个块。 |
{! @foreach ($vars as $var) !}{! @endforeach !} | 控制结构(循环,如果语句,...)。所有PHP控制结构均可使用( if else , elseif , do , while , for ,for, foreach , continue ,继续, switch , break , return , require , include )具有相同的语法,但如果控制结构是标签中的第一个单词,则只需将@符号前缀。 |
{! $var = '' !} | 可变分配。内容可以是变量或任何有效的PHP表达式。 |
{{ $var }} | 打印一个变量。内容可以是变量或任何可以施放到字符串的PHP表达式。 |
{{{ $var }}} | 打印一个变量而不会逃脱。内容可以是变量或任何可以施放到字符串的PHP表达式。 |
{# This is a comment #} | 评论一些东西。这将是PHP评论(最终HTML将不可用)。 |
建议:在Velox主题中查看对PersonsController的persons ,以实现现实的例子。
从v1.4.0开始,Velox带有简单的内置身份验证系统。该系统非常简单易用。
<?php
use MAKS Velox Backend Auth ;
// instantiate the Auth class
$ auth = new Auth (); // or Auth::instance();
// register a new user
$ status = $ auth -> register ( ' username ' , ' password ' );
// unregister a user
$ status = $ auth -> unregister ( ' username ' );
// log in a user
$ status = $ auth -> login ( ' username ' , ' password ' );
// log out a user
$ auth -> logout ();
// authenticate a user model
Auth:: authenticate ( $ user );
// check if there is a logged in user
$ status = Auth:: check ();
// retrieve the current authenticated user
$ user = Auth:: user ();
// add HTTP basic auth
Auth:: basic ([ ' username ' => ' password ' ]);建议:查看UsersController controller以查看一个现实的示例。
Velox是根据MIT许可证许可的开源项目。
版权(C)2021 Marwan al-Soltany。版权所有。