O Punyapp é uma estrutura leve MVC PHP que não requer as extensões de PHP externas, baseia -se na estrutura do CakePHP.
Requer php 5.5.0 ou mais recente.
Layout do diretório de aplicativos:
/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 ' );
}
}Usando declarações preparadas.
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 )
)
);
}
}
Usando modelo PHP puro.
As variáveis de modelo são escapadas para entidades HTML por padrão.
$ this -> view -> text = ' Hello! ' ;
$ this -> view -> render ( ' index ' );Views/Index.php
<html>
<body>
<h1>Sample</h1>
<?php echo $ text ; ?>
</body>
</html>Lidar com eventos de aplicativo ou se defina.
// 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 ;
}
});Validação do formulário de solicitação.
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 '
)
);Extraia arquivos para o diretório qualquer no servidor.
Configurações 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 ' => '' ,
)
); Crie esquema de banco de dados ou escreva esquema no application/settings/app-schema.php .
Defina como gravável nos diretórios e arquivos em application/storage .
Navegue no primeiro diretório extraído de arquivos.
/sample/ .O Punyapp é um software de código aberto licenciado sob a licença do MIT