slim view,自定义渲染方法

slim view, a custom method to render

我跟进 https://github.com/clickcoder/slim-blade 并且它有效,但我想知道是否可以添加自定义方法来调用 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

例如 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);
});

controller/admin/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
  }
}

我还没有深入了解 slim-blade 的内部结构,但我假设 app->render() 方法已经在使用 blade(根据 Slim 的文档和包的名称)。

如果您仍想创建 render_blade() 函数,可以将其添加到 slim 容器 (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 自动加载器和您自己的。