Home Backend Development PHP Tutorial php skymvc 一款轻量、简单的php_php技巧

php skymvc 一款轻量、简单的php_php技巧

May 17, 2016 am 09:17 AM

改框架主要用于实现多个程序员之间的协同开发以及mvc开发模式的实现.skymvc采用mvc开发方式,框架本身易扩展。skymvc作为天网计划的基础框架,秉承易用、易学、共同开发的优良传统,我们致力于打造一款优秀的php
mvc框架。欢迎大家多多提些建议。
1.创建配置文件skyMVC支持自动创建网站目录:输入http://locahost/skymvc/install.php 即可自动创建
文件目录。如果创建之后想重新创建,删除install.lock文件及可。
推荐自动创建。
也可以手动创建:目录都可以自定义
自定义目录时需要对程序进行相应的配置
admin 后台目录
admin/model
admin/ctrl
attach
上传的附件目录
ctrl 控制文件目录
data 目录
data/config.php
配置文件
data/cache 缓存目录
data/cache/css
css缓存
data/cache/file文件缓存
data/cache/tpl 模板缓存
data/cache/js
js缓存
model 模型文件目录
tpl 模板目录
tpl/admin 后台模板
tpl/default
默认模板
js目录
plugin 插件目录
admin.php 后台入口文件
index.php 前台入口文件
2.入口文件


skymvc采用单一入口模式,但不是唯一入口,推荐使用两个入口。一个是前台入口,一个是后台入口。
1.前台入口文件实例:index.php 文件名可以自定义 推荐 index 或者
default

复制代码 代码如下:

require
"data/config.php";//加载配置文件
require("skymvc/skymvc.php");//引用框架文件
//判断控制器是否合法
$_GET['m']=isset($_GET['m'])
&&
in_array($_GET['m'],array('index'))?$_GET['m']:'index';
//判断结束
require_once(CTRL_DIR."/{$_GET['m']}.ctrl.php");
$classname
= $_GET['m'].'Control';
$control = new
$classname();
//配置伪静态的
$control->tpl->rewrite=false;
$control->tpl->rewrite_rule=array(array("/index.php/i"),array("index.html"));
//配置伪静态结束
$method=isset($_GET['a'])
&& method_exists($control,'on'.$_GET['a'])?
'on'.$_GET['a']:"onDefault";
$control->$method();
?>

2.后台入口文件:admin.php 文件名可自定义
复制代码 代码如下:

require
"data/config.php";
require("skymvc/skymvc.php");
$_GET['m']=isset($_GET['m'])
&&
in_array($_GET['m'],array('index','article'))?$_GET['m']:'index';
require_once(ADMIN_DIR."/".CTRL_DIR."/{$_GET['m']}.ctrl.php");
$classname
= $_GET['m'].'Control';
$control = new
$classname();
//配置伪静态的
$control->tpl->tplid="admin";
$control->tpl->currdir="admin";
$control->tpl->rewrite_on=true;
$control->tpl->rewrite_rule=array(array("/index.php/","index.html"));
$method=isset($_GET['a'])
&& method_exists($control,'on'.$_GET['a'])?
'on'.$_GET['a']:"onDefault";
$control->$method()
?>

说明:前后台入口文件的差别不大,主要在于 模型 和 控制文件 所在文件夹。
3.控制器文件
复制代码 代码如下:

class indexControl extends skymvc
{
function
__construct()
{
$this->indexControl();
}

function
indexControl()
{
parent::__construct();//父类初始化
$this->loadModel("index");
//后台

//$this->loadAdminModel("index");
}
function
onDefault()
{

$this->tpl->assign("welcome","欢迎使用skymvc,让我们共同努力!");
$this->tpl->assign("who",$_ENV['indexModel']->test());
//后台
//$this->tpl->assign("who",$_ENV['admin_indexModel']->test());
$this->tpl->display("index");
}
?>

4.模型文件
模型文件主要用于处理数据,当然也可以处理其他的逻辑,但不推荐。文件命名规范:类.model.php
如:index.model.php.
模型文件位于模型目录下面:如model目录
例:index.model.php
复制代码 代码如下:

class
indexModel
{
public $base;
function
__construct(&$base)
{
$this->indexModel($base);
}
function
indexModel(&$base)
{
$this->base=$base;
$this->db=$base->db;
}
function
test()
{
echo "这是模型测试";
}

}
?>

模型文件:前后台一样 就存储的地方不一样
5.hello world
kymvc框架的hello word !
如果是自动创建目录的话。
配置好数据库
index.php
入口文件写好。
index.php内容
复制代码 代码如下:

require
"data/config.php";//加载配置文件
require("skymvc/skymvc.php");//引用框架文件
//判断控制器是否合法
$_GET['m']=isset($_GET['m'])
&&
in_array($_GET['m'],array('index','article'))?$_GET['m']:'index';//将所有在index.php入口出现的模块都放入array()里
//判断结束
require_once(CTRL_DIR."/{$_GET['m']}.ctrl.php");
$classname
= $_GET['m'].'Control';
$control = new
$classname();
$method=isset($_GET['a']) &&
method_exists($control,'on'.$_GET['a'])?
'on'.$_GET['a']:"onDefault";
$control->$method();?>

在ctrl目录下 创建
hello.ctrl.php 文件
复制代码 代码如下:

class
helloControl extends skymvc
{

function __construct()
{
$this->helloControl();
}
function
helloControl()
{
parent::__construct();
$this->loadModel("hello");//载入模型
可以载入任何模型 但不能是相同类的模型
}
//默认执行的动作 命名规范 on函数名
function
onDefault()
{
echo "hello world
"; $this->smarty->display("hello.html");
}
//当m=hello, a=test
执行下面的函数
function
onTest(){
$this->tpl->assign("test",$_ENV['helloModel']->gettest());

$this->tpl->display("hello.html");

}
}?>

在model目录下
创建hello.model.php
复制代码 代码如下:

class helloModel
{
public
$base;
function
__construct(&$base)
{
$this->helloModel($base);
}

function
helloModel(&$base)
{
$this->base=$base;
$this->db=$base->$db;
}
//上面都是不用改的
function gettest(){
return $this->db->getRow("select * from test
limit 1");//读取数据
}
}
?>

在tpl目录下 新建 hello.html
复制代码 代码如下:

BR>PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


content="text/html; charset=gb2312"
/>
无标题文档


这是第一个例子:Hello World !
这是测试的例子:{loop $test $t} {$t}
{/loop}



skymvc 下载地址
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles