PunyApp
1.0.0
PunyApp是一个轻巧的MVC PHP框架,不需要外部PHP扩展,它基于CakePHP框架。
需要PHP 5.5.0或更新。
应用程序目录布局:
/application
/controllers -> App controllers
/models -> App models
/views -> App views
/libraries -> App libraries
/storage -> App storage
/settings -> Application settings
app-settings.php
app-scheme.php
/public -> public web
/css
/js
index.php
/punyapp -> PunyApp libraries
/vendors -> Vendors
index.php
class SampleController extends PunyApp_Controller {
public $ models = array ( ' sample ' );
/**
* GET /login
*/
public function getLogin ( $ params ) {
$ this -> view -> render ( ' sample/login ' );
}
/**
* POST /login
*/
public function postLogin ( $ params ) {
$ has = $ this -> sample -> hasUser ( $ params [ ' id ' ], $ params [ ' pass ' ]);
if ( $ has ) {
$ this -> session -> userid = $ params [ ' id ' ];
$ this -> redirect ( ' home ' );
}
// ...
}
/**
* Any /login
*/
public function anyLogin ( $ params ) {
// ...
}
/**
* Before /login
*/
public function beforeLogin ( $ params ) {
if (! empty ( $ this -> session -> userid )) {
$ this -> redirect ( ' home ' );
}
}
/**
* After /login
*/
public function afterLogin ( $ params ) {
// ...
}
/**
* GET /home
*/
public function getHome ( $ params ) {
if ( empty ( $ this -> session -> userid )) {
$ this -> redirect ( ' login ' );
}
$ this -> view -> user = $ this -> sample -> getUser ( $ this -> session -> userid );
$ this -> view -> render ( ' sample/home ' );
}
/**
* GET /register
*/
public function getRegister ( $ params ) {
$ this -> view -> render ( ' sample/register ' );
}
/**
* POST /register
*/
public function postRegister ( $ params ) {
if ( $ this -> validate ()) {
$ this -> sample -> addUser ( $ params [ ' id ' ], $ params [ ' email ' ], $ params [ ' pass ' ]);
$ this -> session -> userid = $ params [ ' id ' ];
$ this -> redirect ( ' home ' );
}
$ this -> view -> render ( ' sample/register ' );
}
}使用准备好的语句。
class SampleModel extends PunyApp_Model {
public function addUser ( $ userid , $ email , $ pass ) {
$ sample = $ this -> newInstance ();
$ sample -> userid = $ userid ;
$ sample -> email = $ email ;
$ sample -> pass = sha1 ( $ pass );
return $ sample -> save ();
}
public function deleteUser ( $ userid ) {
return $ this -> delete (
array ( ' userid ' => ' ? ' ),
array ( $ userid )
);
}
public function getUser ( $ userid ) {
return $ this -> findOne (
array (
' fields ' => array ( ' id ' , ' userid ' , ' email ' ),
' where ' => array ( ' userid ' => ' ? ' )
),
array ( $ userid )
);
}
public function hasUser ( $ userid , $ pass ) {
return $ this -> has (
array (
' where ' => array (
' userid ' => ' :userid ' ,
' pass ' => ' :pass '
)
),
array (
' :userid ' => $ userid ,
' :pass ' => sha1 ( $ pass )
)
);
}
}
使用纯PHP模板。
默认情况下,HTML实体逃脱了模板变量。
$ this -> view -> text = ' Hello! ' ;
$ this -> view -> render ( ' index ' );视图/index.php
<html>
<body>
<h1>Sample</h1>
<?php echo $ text ; ?>
</body>
</html>处理应用程序事件,或定义自己。
// Handle the database error
$ this -> event -> on ( ' app-database-error ' , function ( $ app , $ error ) {
if ( $ app -> isDebug ()) {
// Show error message only in debug mode
echo $ error ;
}
});请求表格验证。
public $ validationRules = array (
' id ' => array (
' required ' => true ,
' rule ' => array ( ' regex ' , ' /^[a-z0-9]{1,10}$/i ' ),
' message ' => ' Only letters and integers, max 10 characters '
),
' email ' => array (
' required ' => true ,
' rule ' => array ( ' email ' ),
' message ' => ' Invalid email address '
),
' pass ' => array (
' required ' => true ,
' rule ' => array (
array ( ' minLength ' , 4 ),
array ( ' maxLength ' , 20 )
),
' message ' => ' Min 4 characters, max 20 characters '
)
);将文件提取到服务器上的任何目录。
设置application/settings/app-settings.php 。
$ settings = array (
/**
* System settings
*/
' system ' => array (
/**
* Timezone
*
* e.g., 'America/Chicago', 'Asia/Tokyo' etc.
*/
' timezone ' => '' ,
),
/**
* Database settings
*/
' database ' => array (
/**
* Database engine
*
* Available engines: "mysql", "pgsql", "sqlite" and "posql".
*/
' engine ' => '' ,
),
/**
* Session settings
*/
' session ' => array (
/**
* Session engine
*
* Available engines: "php", "file" and "database".
*/
' engine ' => '' ,
)
);创建数据库架构或在application/settings/app-schema.php中编写架构。
设置在application/storage下的目录和文件中。
浏览您提取的第一个文件目录。
/sample/中有样本登录表格。PunyApp是根据MIT许可证许可的开源软件