简体   繁体   English

在PHP中实现控制器的最佳方法是什么?

[英]What is the best way to implement a controller in PHP?

In the last couple of websites I made, I implemented a kind of MVC-style controller, I think. 我认为,在我制作的最后两个网站中,我实现了一种MVC样式的控制器。

I used mod_rewrite to send everything through index.php, so the url became a querystring. 我使用mod_rewrite通过index.php发送所有内容,因此url成为查询字符串。

It worked, but I'm wondering if it's a bit hacky, or just the accepted way of doing things. 它奏效了,但我想知道这是不是有点古怪,还是只是一种公认​​的做事方式。 Is there a better way? 有没有更好的办法? I don't want a framework, I want to learn to do it myself. 我不需要框架,我想自己学习。

Try my smallest framework in the world. 尝试世界上最小的框架。

<?php
$g=$_GET;$c=@$g['c']?$g['c']:'Home';
if(!@include"c/$c.php")die('fail');
$m=method_exists($c,$m=@$g['m'])?$m:'index';
$o=new$c();$o->$m($g);

That goes in index.php and your controlls are Blog.php in ./c/Blog.php. 那在index.php中,而您的控件是./c/Blog.php中的Blog.php。

class Blog {

    function index()
    {
        echo "Hello world";
    }

    function otherpage()
    {
        echo "ZOMG!";
    }

Made mainly as a joke, as I wanted to make a framework that could fit in a tweet, but the basic logic is there ;-) 我主要是开个玩笑,因为我想制作一个可能适合推文的框架,但基本逻辑就在那里;-)

Passing everything through a single point of entry, eg index.php is not MVC, it is the FrontController Pattern. 通过单个入口点传递所有内容,例如index.php不是MVC,它是FrontController模式。 This goes well with MVC though. 不过,这与MVC配合得很好。 See my related answer here . 在这里查看我的相关答案

Apart from that, why not check out some of the frameworks around to learn how they do it. 除此之外,为什么不看看周围的一些框架来学习它们是如何做到的。 Doesn't mean you have to use them, just look at their code and adapt for your own framework. 并不意味着您必须使用它们,只需查看它们的代码并适应您自己的框架即可。

That's the way I accomplished it. 那就是我完成它的方式。 I then created a dispatch table that new, based on URL, which controller to instantiate and which dispatch to run. 然后,我根据URL,要实例化的控制器和要运行的调度创建了一个新的调度表。

How about learning to do it yourself, but still use a framework ? 如何学习自己动手做,但仍然使用框架? Either way, take a look at an open source framework like Symfony or CMS apps like Wordpress, Jommla! 无论哪种方式,都请看一下像Symfony这样的开源框架或像Wordpress,Jommla这样的CMS应用程序! etc, and you will find that they all use mod_rewrite to set things off. 等等,您会发现它们都使用mod_rewrite来设置。

Most PHP frameworks make use of mod_rewrite do accomplish the same objective, and it's the only way to suppress index.php and make the urls more friendly, in a segmented way. 大多数PHP框架都使用mod_rewrite来达到相同的目的,这是抑制index.php并使URL更加分段的唯一方法。

I'd say you're in the right path. 我会说你在正确的道路上。

That method you used is called FrontController Pattern , and it's used by those frameworks as well, to go along with the MVC pattern. 您使用的该方法称为FrontController Pattern ,这些框架也将其与MVC模式一起使用。

If you care for a suggestion, I'd recommend you to make every request pass through each page controller, extending a base controller, since every site has some base data structures that you will probably need to use in every page, such as templates and session control. 如果您有建议,我建议您使每个请求都通过每个页面控制器,扩展一个基本控制器,因为每个站点都有一些基本数据结构,您可能需要在每个页面中使用它们,例如模板和会话控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PHP中实现longpolling的最佳方法是什么? - What is the best way to implement longpolling in PHP? 用PHP实现2向加密的最佳方法是什么? - What is the best way to implement 2-way encryption with PHP? 在php / mysql中搜索拼写错误的最佳方法是什么? - What's the best way to implement typo correction into a search in php/mysql? 实现动态CSS和SVG值(PHP)的最佳方法是什么? - What is the best way to implement dynamic CSS & SVG values (PHP)? 在此PHP应用程序中实现用户首选项的最佳方法是什么? - What's the best way to implement user's preferences in this PHP app? 在PHP中执行的每个方法中实现日志的最佳方法是什么? - What is the best way to implement a log in every method executed in PHP? 在PHP中实现多语言的最佳方法 - Best way to implement multilanguage in PHP 在CakePHP 3.0中,实现不遵循CakePHP命名约定的Controller and Function的最佳方法是什么? - In CakePHP 3.0 what is the best way to implement a Controller and Function that doesn't follow CakePHP naming conventions? 实现 Web 服务登录的最佳方法是什么? - What is the best way to implement login for a webservice? 对 MYSQL 数据库实施版本控制的最佳方法是什么? - What is the best way to implement versioning to a MYSQL database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM