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
PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor