光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>