dongmu3187 2014-11-15 21:33 采纳率: 0%
浏览 1299

PHP:在此服务器上找不到请求的URL /读取

作为php 新手,有人建议我一步一步地执行这个教程:http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html它对于初学者来说简直太友好了,我还创建了我的第一个博客。但当我试图获取 “show”界页时,它会显示一个通过 id 查询参数识别的博客帖子,浏览器显示:

requested URL /read was not found on this server.' I don't use .htaccess and rewrite mode of apache (version of server - Apache/2.2.22) is enable.

代码如下:

Front Controller

<?php
// index.php

// load and initialize any global libraries
require_once 'model.php';
require_once 'controllers.php';

// route the request internally
$uri = $_SERVER['REQUEST_URI'];
if ('/index.php' == $uri) {
    list_action();
} elseif ('/index.php/show' == $uri && isset($_GET['id'])) {
    show_action($_GET['id']);
} else {
    header('Status: 404 Not Found');
    echo '<html><body><h1>Page Not Found</h1></body></html>';
}

Controllers.php

function list_action()
{
    $posts = get_all_posts();
    require 'templates/list.php';
}

function show_action($id)
{
    $post = get_post_by_id($id);
    require 'templates/show.php';
}

Model

<?php
// model.php
function open_database_connection()
{
    $link = mysql_connect('localhost', 'root', 'zoob');
    mysql_select_db('post', $link);

    return $link;
}

function close_database_connection($link)
{
    mysql_close($link);
}

function get_all_posts()
{
    $link = open_database_connection();

    $result = mysql_query('SELECT id, title FROM post', $link);
    $posts = array();
    while ($row = mysql_fetch_assoc($result)) {
        $posts[] = $row;
    }
    close_database_connection($link);

    return $posts;
}

function get_post_by_id($id)
{
    $link = open_database_connection();

    $id = intval($id);
    $query = 'SELECT date, title, post FROM post WHERE id = '.$id;
    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);

    close_database_connection($link);

    return $row;
}

Templates

//layout.php
<!DOCTYPE html>
<html>
<head>
    <title><?php echo $title ?></title>
</head>
<body>
<?php echo $content ?>
</body>
</html>

//list.php

<?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' ?>

//show.php
<?php

$title = $post['title'] ?>

<?php ob_start() ?>
    <h1><?php echo $post['title'] ?></h1>

    <div class="date"><?php echo $post['date'] ?></div>
    <div class="body">
        <?php echo $post['body'] ?>
    </div>
<?php $content = ob_get_clean() ?>

<?php include 'layout.php' ?>

请帮我解决这个问题吧,谢谢!

  • 写回答

2条回答 默认 最新

  • douyi0219 2014-11-15 21:44
    关注

    You have a mismatch in your routes and how you refer to them.

    • In list.php you refer to /read?id=
    • In index.php you defined /show?id=

    Change either of the two and you should be good to go.

    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用