使用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。版權所有。