phalcon 模板文件的布局

配置后布局分为四层,下面是访问后截图

访问地址:127.0.0.1/posts/last/

 

站点项目目录截图:

 

代码部分

  /public/index.php

<?php

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');

// Register an autoloader
$loader = new Loader();

$loader->registerDirs(
    [
        APP_PATH . '/controllers/',
        APP_PATH . '/models/',
    ]
);

$loader->register();

// Create a DI
$di = new FactoryDefault();

// Setup the view component
$di->set(
    'view',
    function () {
        $view = new View();
        $view->setViewsDir(APP_PATH . '/views/');

        // Disable several levels
        // 禁用多个级别
        // $view->disableLevel(
        //     [
        //         View::LEVEL_LAYOUT      => true,
        //         View::LEVEL_MAIN_LAYOUT => true,
        //     ]
        // );
        return $view;
    }
);

// Setup a base URI
$di->set(
    'url',
    function () {
        $url = new UrlProvider();
        $url->setBaseUri('/');
        return $url;
    }
);

// Setup the database service
$di->set(
    'db',
    function () {
        return new DbAdapter(
            [
                'host'     => '127.0.0.1',
                'username' => 'root',
                'password' => 'root3306',
                'dbname'   => 'kgooer_cms',
            ]
        );
    }
);

$application = new Application($di);

try {
    // Handle the request
    $response = $application->handle();

    $response->send();
} catch (\Exception $e) {
    echo 'Exception: ', $e->getMessage();
}

 

app/controllers/PostsController.php

<?php

use Phalcon\Mvc\Controller;

class PostsController extends Controller
{
    public function initialize()
    {
        $this->view->setTemplateAfter('common');
        //$this->view->setTemplateBefore('common');
    }

    public function lastAction()
    {
        $this->flash->notice(
            'These are the latest posts'
        );
    }
}

 

app/views/layouts/common.phtml

<div style="width:700px; height: 400px; background-color: #eee; margin-left: 20px;">
<h6>views/layouts/common.phtml</h6>
<ul class='menu'>
    <li><a href='/'>Home</a></li>
    <li><a href='/articles'>Articles</a></li>
    <li><a href='/contact'>Contact us</a></li>
</ul>

<div class='content'><?php echo $this->getContent(); ?></div>
<p>页尾1</p>
</div>

 

app/views/layouts/posts.phtml

<div style="width:600px; height: 250px; background-color: #ddd; margin-left: 20px;">
    <h2>This is the "posts" controller layout!</h2>
    <h5>/views/layouts/posts.phtml</h5>

    <?php echo $this->getContent(); ?>
    <p>页尾2</p>
</div>

 

app/views/posts/last.phtml

<div style="width:500px; height: 130px; background-color: #fff; margin-left: 20px;">
<h6>views/posts/last.phtml</h6>
<article>
    <h2>This is another title</h2>
    <p>This is another post content</p>
</article>
</div>

 

posted @ 2019-02-19 16:37  梦缘&江南~  阅读(209)  评论(0)    收藏  举报