- Poor organization: If the application grows, this single file will become increasingly unmaintainable. Where should you put code to handle a form submission? How can you validate data? Where should code go for sending emails?
- Difficult to reuse code: Since everything is in one file, there’s no way to reuse any part of the application for other “pages” of the blog.
Symfony fully integrates Doctrine, a library dedicated to database abstraction and mapping.
PHP的数据访问层。。。
<?php foreach ($posts as $post): ?>
<li>
<a href="/read?id=<?php echo $post['id'] ?>">
<?php echo $post['title'] ?>
</a>
</li>
<?php endforeach ?>PHP 对象,PHP数组,结合在html中,其中required x.hml
// include the HTML presentation code
require 'templates/list.php';//
model-view-controller pattern
//逻辑上也不复杂,看起来很简洁的代码
<?php
require_once 'model.php';
$posts = get_all_posts();
require 'templates/list.php';?/php>
后来View 还可以进一步优化为带有母版的页面
<!-- templates/layout.php -->
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<?php echo $content ?>
</body>
</html>=====给两个变量赋值
<?php $title = 'List of Posts' ?>
<?php ob_start() ?>
<h1>List of Posts</h1>
<ul>
<?php foreach ($posts as $post): ?>
<li>
<a href="/read?id=<?php echo $post['id'] ?>">
<?php echo $post['title'] ?>
</a>
</li>
<?php endforeach ?>
</ul>
<?php $content = ob_get_clean() ?>
<?php include 'layout.php' ?>++++++++++++++++++++++++++++++++++++++++++
With index.php as the front controller
/index.php => Blog post list page (index.php executed)
/index.php/show => Blog post show page (index.php executed)前端控制器。即基本的控制器工作在这里实现,可能具体的控制器中代码就比较少了。
When using a front controller, a single PHP file (
index.php
in this case) renders every request. For the blog post show page,/index.php/show
will actually execute theindex.php
file, which is now responsible for routing requests internally based on the full URI. As you’ll see, a front controller is a very powerful tool.
==
With one file handling all requests, you can centralize things such as security handling, configuration loading, and routing
With one file handling all requests, you can centralize things such as security handling, configurat
No error-checking: What if the connection to the database fails?