我跟进 https://github.com/clickcoder/slim-blade 它有效,但我想知道是否可以添加一个自定义方法来调用刀片,而不是使用默认render()
作曲家.json
{
"require": {
"slim/slim": "*",
"clickcoder/slim-blade": "dev-master"
}
}
索引.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
例如路线.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
}
}
我还没有进入细刀片内部,但我假设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 自动加载器和您自己的