0

我目前正在设置我的网站,主要是使用 php。虽然这是我第一次使用它,但我遇到了一些问题。

我已经掌握了网站的基础知识。注册、登录、个人资料页面等 然而这似乎是我必须对网站布局做出决定的地方。

例如。当前,用户的个人资料页面的 URL 为

mysite.com/profile.php

理想情况下,我希望它是类似的东西

mysite.com/user/ChrisSalij

通过阅读这篇文章,我相信我需要一个前端控制器风格的网站,尽管我不确定这一点,也不确定从哪里开始实施。

请记住,我对 php 之类的东西还很陌生,如果有任何有用的评论、链接和建设性的批评,我将不胜感激。

我非常愿意学习,因此文章和解释的链接会非常好。我通常会对这样的事情进行大量研究。但我对它太陌生了,我不知道从哪里开始。

编辑:我还应该补充一点,我计划将这个网站扩大到大规模。一开始它很小,但如果我的目标成功的话,应该会有很多页。所以我愿意现在就努力让它长期正确设置。谢谢

4

8 回答 8

5

Well, welcome to the world of PHP :)

First of all, a front controller is typically only 1 part of a larger framework known as a MVC (Model-View-Controller). Simply put, a front controller can be though of as the "index" page that all people go to when they come to your site. It handles initiating needed site things and then pulling and running what resouces are needed to handle the user request (typically through the URL, as you have given of mysite.com/user/...). This is an overly simple explanation.

While not necessarily a hard thing to learn, I would recommend looking at a tutorial like this that explains the whole idea and basic implementation of a MVC. They call the front controller a "router" (that's another thing, there are more than 1 way to implement a MVC or it's variants and more than 1 name for the different parts). I don't think it is particularity hard to understand or grasp. Most modern MVC frameworks do implement Object Oriented Programming practices. For a good set of video screencast on PHP (including some basic OOP skills), take a look here.

Lastly, if this is your first big use of PHP and want to implement something like MVC, you might check out something like CakePHP or CodeIgniter. Great frameworks that have good documentation and has done alot of hard work for you. Good luck

于 2009-07-03T22:19:29.963 回答
5

Assuming you're on apache, you can create a file called .htaccess at the root of your site, and add these lines

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.php?url=$0 [L,QSA]

This will pass all page requests to index.php. In index.php, you'd want to to parse $_GET['url'] and load up the proper page with include. You'll have to sanitize the input and make sure people aren't including anything they shouldn't. You can get the pieces with something like:

list($controller, $username) = explode('/', $_GET['url']);

The typical MVC structure would use controller/action/id. If "action" is omitted though, as in your example, I'd make it default to "view". As in "view" user's profile. The ID would be the username. Typically, each controller is a class, and each action is a function in that class, any parameters after that are passed into the function. There's also an associated view file with each action.

It's a lot of code to give you a full example (I just coded up an MVC framework!) but that should give you the basics to get started.

Definately check out some other frameworks like CakePHP, Kohana/CodeIgniter if you need more details and code examples.

于 2009-07-03T22:29:59.897 回答
2

Creating a "Front Controller style site" would mean

  1. Using mod_rewrite to intercept all requests into your website/application

  2. Mapping that URL to a PHP class name (your controller) and a method on that controller (typically called action.

However, you don't want to be using PHP directly for this, you want to use either a PHP/MVC Framework, or a PHP based CMS. Example include Joomla, Concrete5, Code Igniter and PHP Cake. This is a "solved" problem.

All of these frameworks have already done the hard work of (among other things) deciding when/how a URL is transformed into a PHP class. By picking one you can ignore re-implementing the wheel and concentrate on your core business (the site you're building).

That's not to say there isn't room for a new framework, either built from scratch or one that combines modules from some other framework (such as the excellent Zend Framework). However, the fact that you're asking such a basic question means you're probably not experienced enough to be the person who should build that (don't feel bad, no one magically has that kind of experience, it only comes with time)

Get some experience under your belt with the existing frameworks, see how they're built and get a feel for how you use them. Then "later", once you have a bunch of real world experience under your belt, if you still feel the need to build your own framework you'll be in a better position to tackle the problem.

于 2009-07-03T23:01:08.977 回答
1

查看URL Rewriting或 Apache 的 mod_rewrite。

于 2009-07-03T22:13:26.577 回答
1

I don't know how much knowledge of PHP you have in general. What I can definitely recommend you is downloading and reading the book PHP 5 Power Programming. You can download it for free here. It takes a lot of time to read it but it will definitely help you a lot. (You can just read selective chapters too.)

Another thing I can recommend you is to read the Quick Start Guide from the Zend Framework.(The framework itself is probably too much for you.) But in this guide especially the links to all the external sites are very good. You can learn a lot of theory from it.

And what all the others said: Learn from all the established frameworks.

于 2009-07-03T23:29:42.367 回答
1

Have a look at Apache's mod_rewrite. Something as simple as the code below would do the trick. I wouldn't make a decision about architecture, if it could be a case that learning to use mod_rewrite would solve your problem. More than likely it will be useful in the future for you anyway.

RewriteRule ^user/(.*)/$    profile.php?username=$1 [PT]

Edit: The Front Controller pattern does not scale. For example if one part of your application is more heavily used than others, if you are using a front controller you can not scale that particular request.

于 2010-12-06T18:50:02.053 回答
0

您应该研究一下 Zend Framework 之类的东西,它为您提供了开箱即用的功能。

They have an excellent guide which will get you going in no time.

http://framework.zend.com/docs/quickstart

于 2009-07-03T22:17:01.280 回答
0

I agree with Sev, this can also be handled with url rewriting. You should also look into frameworks like CakePHP and CodeIgniter that do some things like this for you automatically.

于 2009-07-03T22:17:14.710 回答