语法错误,意外的文件结尾 - cakePHP

时间:2017-01-16 23:32:34

标签: php html cakephp cakephp-3.0

我正在使用cakePHP,它向我显示语法错误,意外的文件结尾作为错误。在我已经做过的研究中,php标签有问题,但我仍然找不到它。

下面是代码

<!DOCTYPE html>
<html>
    <head>

    <?= $this->Html->charset() ?>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <title>SSS</title>
        <?= $this->Html->css('main') ?>

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">
        </script>

</head>

<body>
    <header>

        <div class="container">
            <h1>SSS</h1>
        </div>

    </header>

    <?php
    if($authenticated_user)
    {
        echo '<div class = "logged">Logged - '.$authenticated_user->email.'</div>';
    } ?>

    <nav class = "navbar navbar-default">
        <div class = "container">
            <ul class = "nav navbar-nav">
                <li>
                    <?php
                    echo $this->Html->link('Home', ['controller' => 'Pages', 'action' => 'index']);
                    ?>
                </li>

                <li>
                    <?php
                    echo $this->Html->link('About', ['controller' => 'Pages', 'action' => 'about']);
                    ?>
                </li>

                <li>
                    <?php
                    echo $this->Html->link('Add User', ['controller' => 'Users', 'action' => 'add']);
                    ?>
                </li>

                    <?php
                        if($authenticated_user) 
                        { ?>
                            <li>
                                <?php
                                    echo $this->Html->link('Users', ['controller' => 'Users', 'action' => 'index']);
                                ?>   
                            </li>

                            <li>
                                <?php
                                    echo $this->Html->link('Add Diary Entry', ['controller' => 'Users', 'action' => 'diary']);
                                ?>
                            </li>

                            <li>
                                <?php
                                    echo $this->Html->link('Your Entries', ['controller' => 'Pages', 'action' => 'yours']);
                                ?>  
                            </li>

                            <li>
                                <?php
                                    echo $this->Html->link('Log Out', ['controller' => 'Users', 'action' => 'logout']);
                                ?>
                            </li> <?php
                        } 

                        else
                        { ?>
                            <li>
                                <?php
                                    echo $this->Html->link('Log In', ['controller' => 'Users', 'action' => 'login']);
                                ?>
                            </li>
                        }                   
            </ul>
        </div>
    </nav>

    <div class="container">
                <?php 
            if($message = $this->Flash->render()) 
            {
                echo '
                <div class="alert alert-warning">';
                echo $message;
                echo '</div>';
           ?> }   


    <?= $this->fetch('content') ?>
    </div>

        <footer>
            Copyright &copy; 2016/17  
        </footer>
</body>
</html>

我感谢每一个帮助。非常感谢提前!

2 个答案:

答案 0 :(得分:2)

最后两个if条款的结束括号不在<?php ?>区块中。

如果您希望在HTML中嵌入PHP,则应使用alternative syntax。在这种情况下阅读起来要容易得多。

例如:

<?php if($authenticated_user): ?>
        <li>
            <?=$this->Html->link('Users', ['controller' => 'Users', 'action' => 'index']);?>   
        </li>
<?php else: ?>
        <li>
            <?=$this->Html->link('Log In', ['controller' => 'Users', 'action' => 'login']);?>
        </li>
<?php endif; ?>

答案 1 :(得分:0)

尝试。其实我刚刚纠正了你的语法错误。但为了有效地做到这一点,请更喜欢@ miken32的帖子。

<!DOCTYPE html>
<html>
    <head>
    <?= $this->Html->charset() ?>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>SSS Assignment</title>
        <?= $this->Html->css('main') ?>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">
        </script>

</head>

<body>
    <header>

        <div class="container">
            <h1>SSS</h1>
        </div>

    </header>

    <?php
    if($authenticated_user)
    {
        echo '<div class = "logged">Logged - '.$authenticated_user->email.'</div>';
    } ?>

    <nav class = "navbar navbar-default">
        <div class = "container">
            <ul class = "nav navbar-nav">
                <li>
                    <?php
                    echo $this->Html->link('Home', ['controller' => 'Pages', 'action' => 'index']);
                    ?>
                </li>

                <li>
                    <?php
                    echo $this->Html->link('About', ['controller' => 'Pages', 'action' => 'about']);
                    ?>
                </li>

                <li>
                    <?php
                    echo $this->Html->link('Add User', ['controller' => 'Users', 'action' => 'add']);
                    ?>
                </li>

                    <?php
                        if($authenticated_user) 
                        { ?>
                            <li>
                                <?php
                                    echo $this->Html->link('Users', ['controller' => 'Users', 'action' => 'index']);
                                ?>   
                            </li>

                            <li>
                                <?php
                                    echo $this->Html->link('Add Diary Entry', ['controller' => 'Users', 'action' => 'diary']);
                                ?>
                            </li>

                            <li>
                                <?php
                                    echo $this->Html->link('Your Entries', ['controller' => 'Pages', 'action' => 'yours']);
                                ?>  
                            </li>

                            <li>
                                <?php
                                    echo $this->Html->link('Log Out', ['controller' => 'Users', 'action' => 'logout']);
                                ?>
                            </li> <?php
                        } 

                        else
                        { 
                            ?>
                            <li>
                                <?php
                                    echo $this->Html->link('Log In', ['controller' => 'Users', 'action' => 'login']);
                                ?>
                            </li>
                        <?php
                        } 
                        ?>                  
            </ul>
        </div>
    </nav>

    <div class="container">
                <?php 
            if($message = $this->Flash->render()) 
            {
                echo '
                <div class="alert alert-warning">';
                echo $message;
                echo '</div>';
            }  ?> 


    <?= $this->fetch('content') ?>
    </div>

        <footer>
            Copyright &copy; 2016/17  
        </footer>
</body>
</html>`