เฟรมเวิร์ก PHP Light
composer install php >= 5.6 composer
สิ่งนี้ขับเคลื่อนโดย Blade ความช่วยเหลือบางอย่างที่นี่: 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หลังจากเขียนไลบรารี/คอนโทรลเลอร์ของคุณเอง (ฯลฯ ) โปรดตรวจสอบให้แน่ใจว่าคุณได้เรียกใช้คำสั่งนี้:
composer dump-autoloadLongphp ขับเคลื่อนโดยนักแต่งเพลงซึ่งสามารถช่วยคุณขยายแอปพลิเคชันของคุณโดยใช้เนมสเปซ
ผู้ควบคุม
<?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
| การกำหนดค่า | ตัวเลือก | คำอธิบาย |
|---|---|---|
db_host | ชื่อโฮสต์ฐานข้อมูล | |
db_user | ชื่อผู้ใช้ฐานข้อมูล | |
db_password | Database Pass | |
db_port | 3306/(อื่น ๆ ) | พอร์ตฐานข้อมูล |
db_database | ฐานข้อมูลฐานข้อมูล | |
db_charset | UTF8/(อื่น ๆ ) | ฐานข้อมูล |
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 และใช้เป็นค่าเริ่มต้น
| การกำหนดค่า | คำอธิบาย |
|---|---|
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
| การกำหนดค่า | ตัวเลือก | คำอธิบาย |
|---|---|---|
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 ();การสร้างรหัสเซสชันใหม่มักจะทำเพื่อป้องกันไม่ให้ผู้ใช้ที่เป็นอันตรายจากการใช้ประโยชน์จากการโจมตีเซสชันการโจมตีแอปพลิเคชันของคุณ
Longphp สร้าง ID เซสชันใหม่โดยอัตโนมัติ อย่างไรก็ตามหากคุณต้องการสร้าง ID เซสชันใหม่ด้วยตนเองคุณอาจใช้วิธีการงอกใหม่
LongPHP:: regenerate ();Opertations คุกกี้:
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 ' );ระดับของ logger:
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
| การกำหนดค่า | ตัวเลือก | คำอธิบาย |
|---|---|---|
log_level | 0/1/2/3/4 | ระดับบันทึก |
log_path | สถานที่จัดเก็บไฟล์บันทึก |
0 อย่าทำบันทึกใด ๆ 1 บันทึกข้อผิดพลาด 2 บันทึกข้อผิดพลาด、 คำเตือน、 ประกาศ 3 ข้อผิดพลาด、 คำเตือน、 ประกาศ、 ข้อมูล 4 บันทึกทั้งหมด
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 ใหม่โปรดตรวจสอบให้แน่ใจว่าคุณได้เปิดใช้งาน 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>