迷你PHP框架 - 使用标准MVC结构快速创建应用程序。
leia naversãoemportuguês(pt-br):portuguêspt-br
为什么要使用Codemini?
如果您不想出于任何原因而使用复杂的结构,并且不想将其绑定,那么Codemini是您的选择。
CodeMini非常易于使用,您可以将Projet运行在Laravel,Codeigniter 4等public文件夹中,或者如果您使用共享主机,则只需将index.php和.htaccess从公共文件夹复制到root文件夹中,并且所有事情都可以正常工作。
您可以轻松地将来自Packagist.org的其他软件包进入您的项目,只需运行composer require <vendor>/<package> ,CodeMini就会了解您已安装的所有软件包。
Wamp或Xampp等第三方工具
如上所述,如果您使用的是共享主机或使用Wamp或Xampp等工具,则只需将index.php和.htaccess从公共文件夹复制到根文件夹中,并且所有事情都可以正常工作。
例子:
www或htdocsindex.php和.htaccess从公共文件夹复制到root文件夹中注意:如果需要,请删除公共文件夹
为什么这样做?
因为当您使用Wamp或XAMPP等工具时,Apache的DocumentRoot是根文件夹www(Wamp)或HTDOCS(XAMPP),而不是框架的公共文件夹。
该规则不仅对Codemini有效,而且对Codeigniter 4,Laravel等。这是框架工作的方式。
1-如果要安装作为作曲家项目,请运行: composer create-project --prefer-dist codemini/framework name-folder-of-you-project
2-开放式终端和运行Cli-Tools: php cli-tools serve
可选:使用PHP内置服务器运行,转到public文件夹并运行: php -S localhost:8080
注意:在这种情况下,不需要运行作曲家安装,因为作曲家创建项目已经为您完成。
1-如果要使用git克隆安装,请运行: git clone https://github.com/fabriciopolito/Codemini.git或下载“下载zip”并提取文件。
2-在Root Project文件夹中运行Composer(必需),其中包含Composer.json以创建自动加载文件。
composer installphp composer.phar install 3-开放式终端和运行Cli-Tools: php cli-tools serve
可选:使用PHP内置服务器运行,转到public文件夹并运行: php -S localhost:8080
您的index.php应该看起来像这样:
<?php
$ dirname = strtolower ( basename ( __DIR__ ));
if ( $ dirname == ' public ' ) {
require_once ' ../app/Init.php ' ;
} else {
require_once ' app/Init.php ' ;
}
try {
$ myAPP = new Init ();
} catch ( Exception $ e ) {
$ e -> getMessage ();
} //end try...catch 注意: Codemini没有很多配置。
修改标准文件:
Config.php将config定义为base_url,mysql,环境,时区等例子:
$ config [ ' base_url ' ] = ' http://localhost:8080/ ' ;
$ config [ ' environment ' ] = ' development ' ;
$ config [ ' mysql ' ] = [
' host ' => ' localhost ' ,
' dbname ' => ' codemini_tests ' ,
' username ' => ' root ' ,
' password ' => '' ,
' charset ' => ' utf8 ' ,
' display_error ' => ( $ config [ ' environment ' ] == ' development ' ) ? true : false
];
$ config [ ' session_name ' ] = ' MY_Session_name_ ' ;
$ config [ ' timezone ' ] = ' America/Sao_Paulo ' ;
$ config [ ' page_not_found ' ] = ' PageNotFound@index ' ;
$ config [ ' view_extension ' ] = ' .phtml ' ;注意:文件应用程序/config.php具有完整的文档
Constants.php定义您的项目名称和文件位置...并创建您的控制器,视图和模型!
控制器 / Home.php
php cli-tools create-controller Home输出:./app/controllers/home.php
<?php
namespace App Controllers ;
use Codemini Core Controller ;
use Codemini Core Request ;
class Home extends Controller{
public function __construct (){
parent :: __construct ();
}
public function index ( $ args = "" ){
//Data to view
//Example: $this->view->data = ['php', 'js', 'nodejs', 'mongodb', 'css'];
//Load view
//$this->view('template_name');
echo " Controller name: " . Request:: getController () . " <br> " ;
echo " Method name: " . Request:: getMethod () . " <br> " ;
}
}视图 / Template/index.phtml
<!doctype html >
< html lang =" en " >
< head >
<!-- Required meta tags -->
< meta charset =" utf-8 " >
< meta name =" viewport " content =" width=device-width, initial-scale=1, shrink-to-fit=no " >
< base href =" <?php echo $config['base_url'] ?> " >
<!-- Bootstrap CSS -->
< link rel =" stylesheet " href =" https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css " integrity =" sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh " crossorigin =" anonymous " >
< title > Application name </ title >
</ head >
< body >
< ?php
print ' < pre > ';
print_r($this- > view- > data);
print ' </ pre > ';
? >
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
< script src =" https://code.jquery.com/jquery-3.4.1.slim.min.js " integrity =" sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n " crossorigin =" anonymous " > </ script >
< script src =" https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js " integrity =" sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo " crossorigin =" anonymous " > </ script >
< script src =" https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js " integrity =" sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6 " crossorigin =" anonymous " > </ script >
</ body >
</ html >型号 / Products.php
php cli-tools create-model Products输出:./app/models/products.php
<?php
namespace App Models ;
use Codemini Core Model ;
class Products extends Model{
protected $ table = ' table_name ' ;
/**
* Construct the parent model class for get instance '$this->db' PDO and the
* SIMPLE QUERY BUILDER functions
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Example 1 with VERY SIMPLE query builder
*/
public function allProducts ( $ orderBy = " ORDER BY `name` ASC " ){
$ sql = " SELECT * FROM ` { $ this -> table } ` { $ orderBy }" ;
$ this -> query ( $ sql );
$ this -> execute ();
return $ this -> fetchAll ();
}
/**
* Example 2 with VERY SIMPLE query builder
*/
public function productById ( $ val )
{
$ sql = " SELECT * FROM ` { $ this -> table } ` WHERE `id` = :id " ;
$ this -> query ( $ sql );
$ this -> bind ( " :id " , $ val );
$ this -> execute ();
return $ this -> fetch ();
}
/**
* Example 3 with VERY SIMPLE query builder
*/
public function productsByPrice ( $ val )
{
$ sql = " SELECT * FROM ` { $ this -> table } ` WHERE `price` = :price " ;
$ this -> query ( $ sql );
$ this -> execute ([ " :price " => $ val ]);
return $ this -> fetchAll ();
}
/**
* Example 4 with MANUALLY statement $db
*/
public function productsByName ( $ val )
{
$ sql = " SELECT * FROM ` { $ this -> table } ` WHERE `name` = :name " ;
$ stmt = $ this -> db -> prepare ( $ sql );
$ stmt -> bindParam ( " :name " , $ val , PDO :: PARAM_STR );
$ stmt -> execute ();
return $ stmt -> fetch ();
}
}configItem('key')返回配置指定名称。示例: <?php echo configItem('base_url') ?>
&getInstance()返回控制器对象实例
如何在控制器中使用Librarie?
这很简单!只需加载使用use说明,库将为您提供。
例子:
<?php
namespace App Controllers ;
//IMPORTANT
// Don't forget to load with 'use' instruction
use Codemini Core Controller ;
use Codemini Libraries Input ;
class Teste extends Controller{
public function __construct (){
parent :: __construct ();
}
public function index ( $ args ){
//$_POST
$ email = Input:: post ( ' email ' );
$ password = Input:: post ( ' password ' );
//$_GET
$ email = Input:: get ( ' email ' );
$ password = Input:: get ( ' password ' );
//FILE
$ userfile = Input:: file ( ' userfile ' );
//ALL REQUEST
print_r ( $ allRequest = Input:: all ());
}
}Codemini的基本库
Input - 帮助您操纵获取,发布,文件echo Input::get('email')echo Input::post('email')echo Input::file('userfile')echo Input::all()Redirect - 将用户重定向到其他位置echo Redirect::to(configItem('base_url') . 'login/index')Session - 帮助您操纵会话数据Session::start()Session::set('logged_in', true)Session::set(array('user_id' => 1, 'logged_in' => true))Session::get('user_id')Session::has('logged_in')Session::all()Session::id()Session::regenerateId()Session::remove('user_id')Session::destroy()Validator - 帮助您验证数据Validator::getErrors()Validator::getMsg()Validator::setOpenTag('<p>')Validator::setCloseTag('</p>')Validator::required($val)Validator::isEmail($val)Validator::isUrl($val)Validator::isFloat($val)Validator::isInt($val)Validator::isBool($val)Validator::isIp($val)Validator::regex($val, '/[az]/i')注意:库中每个选项中都有完整的文档。
你有空!因此,例如,在./app/和一个文件Upload.php中创建一个文件夹Helpers ,您唯一要做的就是设置适当的自动加载名称空间。
示例./app/Helpers/Upload.php :
<?php
namespace App Helpers ;
class Upload
{
public static function setUpload ( $ file )
{
//The logic code here...
}
}然后以这种方式使用任何控制器:
示例./app/Controllers/Home.php
<?php
namespace App Controllers ;
use Codemini Core Controller ;
// IMPORTANT:
// Don't forget load the helper librarie you have created
use App Helpers Upload ;
class Home extends Controller{
public function __construct (){
parent :: __construct ();
}
public function index ( $ args = "" ){
// call methods
Upload:: setUpload ( $ _FILE [ ' userfile ' ]);
}
}这很简单!只需运行作曲家需要命令并以上面的方式加载它。
示例1: composer require plasticbrain/php-flash-messages
示例2: composer require monolog/monolog
Fabriciopólito [email protected] -https://github.com/fabriciopolito
谢谢使用吗?
Codemini已获得MIT许可证的许可✔️