search
HomeBackend DevelopmentPHP Tutorialphp 自己写mvc 框架 url重写 等怎么写

求指导


回复讨论(解决方案)

有人指导下思路 有网上的教程也是可以的 

载个开源框架 看看代码不就知道了吗?

apache全定位到一个文件 转发~  

mvc框架 和 url重写 没有直接关系
通常框架都是单入口的,这反倒使url重写变得简单了

去下载个thinkphp看源码啊

一个文件调用
$m=$_GET['m']?ucfirst(strtolower($_GET['m'])):'Index';
$mm=$m;


$m=$m.'Action';

$mod=new $m;

$method=$_GET['a']?$_GET['a']:'index';

if(method_exists($m,$method)){
$mod->$method();
}else{
echo '不存在这个方法';
}



另一个文件路由处理
/*
 * ******************************************************************************/
class Prourl {
/**
 * URL路由,转为PATHINFO的格式
 */ 
static function parseUrl(){
if (isset($_SERVER['PATH_INFO'])){
         //获取 pathinfo
$pathinfo = explode('/', trim($_SERVER['PATH_INFO'], "/"));

        // 获取 control
        $_GET['m'] = (!empty($pathinfo[0]) ? $pathinfo[0] : 'index');

        array_shift($pathinfo); //将数组开头的单元移出数组 
      
        // 获取 action
        $_GET['a'] = (!empty($pathinfo[0]) ? $pathinfo[0] : 'index');
array_shift($pathinfo); //再将将数组开头的单元移出数组 

for($i=0; $i $_GET[$pathinfo[$i]]=$pathinfo[$i+1];
}

}else{
$_GET["m"]= (!empty($_GET['m']) ? $_GET['m']: 'index');    //默认是index模块
$_GET["a"]= (!empty($_GET['a']) ? $_GET['a'] : 'index');   //默认是index动作

if($_SERVER["QUERY_STRING"]){
$m=$_GET["m"];
unset($_GET["m"]);  //去除数组中的m
$a=$_GET["a"];
unset($_GET["a"]);  //去除数组中的a
$query=http_build_query($_GET);   //形成0=foo&1=bar&2=baz&3=boom&cow=milk格式
//组成新的URL
$url=$_SERVER["SCRIPT_NAME"]."/{$m}/{$a}/".str_replace(array("&","="), "/", $query);
header("Location:".$url);
}
}
}


}




不谢!!

没写过框架,表示路过

1.写一个单入口的方式
2.通过基控制器,通过参数来控制相应的控制器和方法名,比如:c为控制器,m为方法
3.通过面向对象自动加载类。
4.想使用mvc开发框架,可以用smarty为内核
5.需要有一个数据库驱动,可以用PDO,最后是做受保护的类,让其它类继承。
6.另外还需要做些组件和小部件,融合在框架中,方便开发者使用,一般小部件都是供视图使用。

Statement
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment