超薄视图,自定义渲染方法

时间:2015-01-17 07:46:11

标签: php blade slim

我跟进https://github.com/clickcoder/slim-blade并且它有效,但我想知道是否可以添加自定义方法来调用刀片,而不是使用默认render()

composer.json

{
    "require": {
        "slim/slim": "*",
        "clickcoder/slim-blade": "dev-master"
    }
}

的index.php

<?php
require 'vendor/autoload.php';
require 'vendor/slim/Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim(array(
    'view' => new \Slim\Views\Blade(),
    'templates.path' => 'project/view',
    'debug' => true,
    'log.enabled' => true
));

$view = $app->view();
$view->parserOptions = array(
  'debug' => true,
  'cache' => 'project/blade_cache'
);

// include the file which contains all the project related includes
spl_autoload_register(function ($class_name) {
  // replace namespace separator with directory separator (prolly not required)
  $class_name = str_replace('\\', DIRECTORY_SEPARATOR, $class_name);
  // get full name of file containing the required class
  $file = __DIR__.DIRECTORY_SEPARATOR.$class_name.'.php';
  // get file if it is readable
  if (is_readable($file)) require_once $file;
});
require 'project/config/database.php';
require 'project/route.php';
$app->run();

文件夹

composer.json
index.php
project
  controller
  model
  route.php
  view  
vendor
  autoload.php
  clickcoder
  composer
  nesbot
  philo
  slim
  symfony

e.g。 route.php

use project\controller\admin as admin_controller;
$app->get('/admin', function() use ($app) {
  $index_controller = new admin_controller\index_controller();
  $index_controller->index($app);
});

控制器/管理/ index_controller.php

<?php
namespace project\controller\admin;
class index_controller {
  public function index($app) {
    // .. get $data from model
    $app->render('admin/index_view.php', array('data' => $data));  // without blade
    $app->blade_render('admin/index_view', array('data' => $data)); // how to do this add a custom method to call blade extension
  }
}

1 个答案:

答案 0 :(得分:1)

我还没有进入纤细的刀片内部,但我认为app->render()方法已经在使用刀片(根据Slim的文档和包的名称)。

如果您仍想创建render_blade()功能,可以将其添加到细长容器(http://docs.slimframework.com/di/overview/

// EXAMPLE #1 ($app->view is the instance of Slim\View\Blade
$app->render_blade = function ($tpl, $data = array()) use ($app) {
    // if this doesn't work look at example #2
    // the "getInstance()" is a method of the class
    // according to slim it could be "echo" instead of return
    // to send it to the object buffer
    return $this->view->getInstance()->make($tpl, $data);
};

// EXAMPLE #2 ($app->view is not available)

// in your code, before $app = new \Slim\Slim(...)
$view = new \Slim\View\Blade();

$app = new \Slim\Slim( array(...configs...,  "view" => $view) );

$app->singleton("blade", function() use ($view){
     return $view->getInstance();
});
// Now you can call $app->blade->make() everywhere!
// or register the render_blade() function as in example #1
// Just use $app->blade instead of $app->view->getInstance()

另外,我建议您查看名称空间,自动加载器,作曲家自动加载和作曲家PSR-4配置。由于你有自动加载器,但仍然需要Slim文件,注册Slim自动加载器和你自己的。