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?
  • 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 the index.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






    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值