光PHP MVC框架
composer installphp >= 5.6 composer
這是由刀片提供動力的,這裡有一些幫助:https://laravel.com/docs/5.3/blade
$ this -> render ( ' your/blade ' , array ( ' your ' => ' variables ' ));請確保您擁有項目的正確訪問根。
chmod -R 0755 /Path/to/Longphp
chmod -R 0644 /Path/to/Longphp/cache
chmod -R 0644 /Path/to/Longphp/Application/logs編寫了自己的庫/控制器(etc)後,請確保您已運行此命令:
composer dump-autoloadLongPhp由作曲家提供動力,可以通過使用名稱空間來幫助您擴展應用程序。
Democontroller
<?php
/**
* Longphp
* Author: William Jiang
*/
namespace Application Controller ;
use Long Core Config ;
use Long Core LongException ;
use Long Library Input ;
use Long Core LongController ;
use Long Library Logger Log ;
use Long Library Session LongSession ;
use Long Library Url ;
class DemoController extends LongController
{
//construct method
public function initialize (){
$ this -> _output ( " initMethod<br> " );
}
public function index ()
{
echo Input:: get ( ' a ' );
echo " index method called<br> " ;
}
public function testRender ()
{
$ this -> _render ( ' test ' , [ ' test ' => ' test here ' ]);
}
protected function testConfig ()
{
print_r (Config:: get ());
}
public function testOutput ()
{
$ this -> _output ([ ' testdata1 ' => ' data1 ' , ' testdata2 ' => ' data2 ' ], ' json ' );
//$this->_output('raw data test<br>' . PHP_EOL, 'raw');
}
public function testUrl ()
{
$ this -> _output (Url:: siteUrl ( ' https ' ) . PHP_EOL , ' raw ' );
}
protected function testError ()
{
//测试捕获异常
new EmptyClass ();
}
protected function testException ()
{
//测试Exception
throw new LongException ( ' Test Exception ' );
}
public function testModel ()
{
$ model = $ this -> _model ( ' testModel ' );
print_r ( $ model -> getById ( 4 ));
var_dump ( $ model -> insertTestData ());
var_dump ( $ model -> deleteTestData ( 2 ));
var_dump ( $ model -> updateTestData ( 1 , '测试' ));
var_dump ( $ model -> transTestData ());
}
public function testLogger (){
Log:: warning ( " test warning " );
}
public function testSession (){
LongSession:: set ( ' test_data ' , 123 );
$ data = LongSession:: get ( ' test_data ' );
var_dump ( $ data );
//LongSession::destroy();
LongSession:: batchSet ([ ' test1 ' => ' data1 ' , ' test2 ' => ' data2 ' ]);
var_dump (LongSession:: all ());
echo ' <br/> ' ;
var_dump (LongSession:: pull ( ' test1 ' ));
LongSession:: remove ( ' test2 ' );
}
}您可以訪問這樣的索引方法:http:// yourhost/index.php/demo/index
在Application/config/database.php中配置數據庫
| config | 選項 | 描述 |
|---|---|---|
db_host | 數據庫主機名 | |
db_user | 數據庫用戶名 | |
db_password | 數據庫通過 | |
db_port | 3306/(其他) | 數據庫端口 |
db_database | 數據庫數據庫 | |
db_charset | UTF8/(其他) | 數據庫charset |
db_driver | mysqli | 數據庫驅動程序,僅支持MySQL |
演示模型:
<?php
/**
* Longphp
* Author: William Jiang
*/
namespace Application Model ;
use Long Core LongModel ;
class TestModel extends LongModel
{
public function getById ( $ id = 1 )
{
return $ this -> db -> query ( ' SELECT * FROM test_table WHERE id = ? ' , array ( $ id ));
}
public function insertTestData ()
{
return $ this -> db -> query ( ' INSERT INTO test_table(`number`,`doubled`,`strings`,`time`) VALUES(?,?,?,?) ' , [ 1 , 1.111 , ' string test ' , date ( ' Y-m-d H:i:s ' )]);
}
public function updateTestData ( $ id = 1 , $ update )
{
return $ this -> db -> query ( ' UPDATE test_table SET `strings` = ? WHERE `id` = ? ' , array ( $ update , $ id ));
}
public function deleteTestData ( $ id = 1 )
{
return $ this -> db -> query ( ' DELETE FROM test_table WHERE id = ? ' , array ( $ id ));
}
public function transTestData (){
$ this -> db -> transStart ();
$ this -> updateTestData ( 1 , ' This is Transaction ' );
$ this -> db -> commit ();
$ this -> db -> transStart ();
$ this -> updateTestData ( 1 , ' This is rollback ' );
$ this -> db -> rollback ();
}
}當您不在URL中找出控制器或方法時,您可以在Application/config/router.php上設置默認控制器和方法,並用作默認情況。
| config | 描述 |
|---|---|
default_controller | 默認控制器 |
default_method | 默認方法 |
獲取輸入數據。
use Long Library Input ; //import Input
. . .
//getting input data
echo Input:: get ( ' key ' );
echo Input:: post ( ' key ' );
echo Input:: put ( ' key ' );
echo Input:: delete ( ' key ' );LongPHP提供會話支持。現在支持會話驅動程序: file , database 。
配置文件存儲在Application/config/config.php
| config | 選項 | 描述 |
|---|---|---|
session_driver | 文件/數據庫 | 在哪裡存儲會話 |
session_path | 框架/會話 | 會話文件位置。留空以使用默認 |
session_cookie_name | 會話令牌商店在瀏覽器中 | |
session_expiration | 7200/(您想要的時間) | 會議的到期時間 |
您可以使用LongSession::get($key)檢索數據。 LongSession::all()檢索您列出的所有數據。
確保您已經使用了命名空間LongLibrarySession
namespace Application Controller ;
use Long Library Session ;
class UserController extends LongController
{
public function show ()
{
$ value = LongSession:: get ( ' key ' );
//if you want to get all data
}
}使用set方法可以幫助您將數據放入會話中。如果您願意放置大量數據, batchSet將很有用。
//put data
LongSession:: set ( ' key ' , ' value ' );
//batch assignment
LongSession:: batchSet ([ ' test1 ' => ' data1 ' , ' test2 ' => ' data2 ' ]);remove方法將從會話中刪除一塊數據。如果您想在刪除數據之前獲取數據,請使用pull 。如果您想從會話中刪除所有數據,則可以使用flush方法:
//delete 'key'
LongSession:: remove ( ' key ' );
//get 'key' then delete it
LongSession:: pull ( ' key ' );
//Delete all data
LongSession:: flush ();經常進行再生會話ID,以防止惡意用戶對您的應用程序進行會話固定攻擊。
longPhp自動將會話ID再生;但是,如果您需要手動再生會話ID,則可以使用重生方法。
LongPHP:: regenerate ();餅乾作用:
use Long Library Cookie
...
//retrive cookie
echo Cookie:: get ( ' test_cookie ' );
//set cookie
Cookie:: set ( ' test_cookie ' , ' cookie value ' , 100 );
//remove cookie
Cookie:: remove ( ' test_cookie ' );記錄器的水平:
const EMERGENCY = ' emergency ' ;
const ALERT = ' alert ' ;
const CRITICAL = ' critical ' ;
const ERROR = ' error ' ;
const WARNING = ' warning ' ;
const NOTICE = ' notice ' ;
const INFO = ' info ' ;
const DEBUG = ' debug ' ;您可以根據log level調用記錄方法
| config | 選項 | 描述 |
|---|---|---|
log_level | 0/1/2/3/4 | 日誌級別 |
log_path | 在哪裡存儲日誌文件。 |
0不要執行任何日誌1日誌錯誤2日誌錯誤,警告,通知3錯誤,警告,
use Long Library Logger Log ; //to import logger
. . . .
Logger:: debug ( " some debug message " ); // static calling
Logger:: info ( " some info message " ); // static calling
Logger:: warning ( " some warning message " ); // static calling
Logger:: notice ( " some notice message " ); // static calling
Logger:: error ( " some error message " ); // static calling
Logger:: critical ( " some critical message " ); // static calling
Logger:: alert ( " some alert message " ); // static calling
Logger:: emergency ( " some emergency message " ); // static calling用法
假設使用此HTML表格上傳文件:
< form method =" POST " enctype =" multipart/form-data " >
< input type =" file " name =" foo " value ="" />
< input type =" submit " value =" Upload File " />
</ form >提交HTML表單後,服務器端PHP代碼可以驗證和上傳文件:
<?php
$ storage = new Upload Storage FileSystem ( ' /path/to/directory ' );
$ file = new Upload File ( ' foo ' , $ storage );
// Optionally you can rename the file on upload
$ new_filename = uniqid ();
$ file -> setName ( $ new_filename );
// Validate file upload
// MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml
$ file -> addValidations ( array (
// Ensure file is of type "image/png"
new Upload Validation Mimetype ( ' image/png ' ),
//You can also add multi mimetype validation
//new UploadValidationMimetype(array('image/png', 'image/gif'))
// Ensure file is no larger than 5M (use "B", "K", M", or "G")
new Upload Validation Size ( ' 5M ' )
));
// Access data about the file that has been uploaded
$ data = array (
' name ' => $ file -> getNameWithExtension (),
' extension ' => $ file -> getExtension (),
' mime ' => $ file -> getMimetype (),
' size ' => $ file -> getSize (),
' md5 ' => $ file -> getMd5 (),
' dimensions ' => $ file -> getDimensions ()
);
// Try to upload file
try {
// Success!
$ file -> upload ();
} catch ( Exception $ e ) {
// Fail!
$ errors = $ file -> getErrors ();
}如果您想將URI縮短, url rewrite將幫助您隱藏index.php在URI中。
server
{
listen 80 ;
server_name www.example.com ;
index index.shtml index.html index.htm index.php ;
root /path/to/root/Longphp ;
location / {
try_files $uri $uri / =404 ;
if ( ! -e $request_filename )
{
rewrite (. * ) /index.php ;
}
}
location ~ . * . (php | php5) ? $
{
fastcgi_pass 127.0.0.1:9000 ;
fastcgi_index index.php ;
include fastcgi.conf ;
}
access_log /var/log/Longphp/access.log access ;
}如果您的項目不是根目錄
例如,您的項目位於/path/to/root/Longphp中
server
{
listen 80 ;
server_name www.example.com ;
index index.shtml index.html index.htm index.php ;
root /path/to/root ;
location /Longphp {
try_files $uri $uri / =404 ;
if ( ! -e $request_filename )
{
rewrite (. * ) /Longphp/index.php ;
}
}
location ~ . * . (php | php5) ? $
{
fastcgi_pass 127.0.0.1:9000 ;
fastcgi_index index.php ;
include fastcgi.conf ;
}
access_log /var/log/Longphp/access.log access ;
}如果您期待重寫URL,請確保已啟用Apache rewrite module 。我們已經在框架目錄下為您編寫了.htaccess文件。
這是Apache的配置。
<VirtualHost *:80>
DocumentRoot "/path/to/Longphp"
ServerName www.example.com
AddType application/x-httpd-php .php
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
DirectoryIndex index.php
</Directory>
</VirtualHost>