Slim Framework 3 - 在全局模板中使用路由器

时间:2016-10-10 20:43:59

标签: php slim

我对Slim Framework 3比较陌生。我想了解的一件事是如何在“全局”模板中使用路由器$this->router

我的意思是这是一个模板,例如导航菜单 - 每个页面上都会显示一些内容。

对于模板我按照我安装的example tutorial使用“php-view”库:

composer require slim/php-view

在我的模板目录中,我有一个名为nav.php的文件,我想输出我的链接。

我理解如何像这样调用路由器

<a href="<?=$router->pathFor('sign-up')?>">Sign Up</a>

但是......示例教程仅显示了如何从1个单独的地方传递该链接,例如$app->get('/sign-up' ... })->setName("sign-up");

如何在任何模板中全局使用路由器,而不将其作为参数传递到每个单独的URL路由中?

我更熟悉像CakePHP这样的框架,其中有一个“AppController”,它允许你设置全局,即在每个请求中都可用。我不知道这是否是在Slim中完成的,但这就是我所追求的效果。

3 个答案:

答案 0 :(得分:2)

好吧,您可以将其作为template variable传递。

当您在容器中实例化或注册PhpRenderer时,您有多个选项来定义&#34; global&#34;变量,即可在所有模板中访问的变量:

// via the constructor
$templateVariables = [
    "router" => "Title"
];
$phpView = new PhpRenderer("./path/to/templates", $templateVariables);

// or setter
$phpView->setAttributes($templateVariables);

// or individually
$phpView->addAttribute($key, $value);

假设您通过Pimple注册PhpRenderer

<?php
// Create application instance
$app = new \Slim\App();

// Get container
$container = $app->getContainer();

// Register PhpRenderer in the container
$container['view'] = function ($container) {

    // Declaring "global" variables
    $templateVariables = [
        'router' => $container->get('router')
    ];

    // And passing the array as second argument to the contructor
    return new \Slim\Views\PhpRenderer('path/to/templates/with/trailing/slash/', $templateVariables);
};

答案 1 :(得分:1)

Starting MySQL 5.7.21-1.1.3
2018-02-11T12:53:22.120561Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-02-11T12:53:22.125276Z 0 [Note] mysqld (mysqld 5.7.21) starting as process 1 ...
2018-02-11T12:53:22.139968Z 0 [Note] InnoDB: PUNCH HOLE support available
2018-02-11T12:53:22.140147Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-02-11T12:53:22.140221Z 0 [Note] InnoDB: Uses event mutexes
2018-02-11T12:53:22.140282Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2018-02-11T12:53:22.140345Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-02-11T12:53:22.140404Z 0 [Note] InnoDB: Using Linux native AIO
2018-02-11T12:53:22.141479Z 0 [Note] InnoDB: Number of pools: 1
2018-02-11T12:53:22.144262Z 0 [Note] InnoDB: Using CPU crc32 instructions
2018-02-11T12:53:22.146215Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2018-02-11T12:53:22.154759Z 0 [Note] InnoDB: Completed initialization of buffer pool
2018-02-11T12:53:22.158172Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2018-02-11T12:53:22.212129Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2018-02-11T12:53:22.304744Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2018-02-11T12:53:22.304989Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2018-02-11T12:53:22.307389Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2018-02-11T12:53:22.469757Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2018-02-11T12:53:22.481187Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2018-02-11T12:53:22.481622Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2018-02-11T12:53:22.482185Z 0 [Note] InnoDB: Waiting for purge to start
2018-02-11T12:53:22.533061Z 0 [Note] InnoDB: 5.7.21 started; log sequence number 2552053
2018-02-11T12:53:22.533556Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2018-02-11T12:53:22.533812Z 0 [Note] Plugin 'FEDERATED' is disabled.
2018-02-11T12:53:22.591600Z 0 [Note] InnoDB: Buffer pool(s) load completed at 180211 12:53:22
2018-02-11T12:53:22.600231Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2018-02-11T12:53:22.600407Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2018-02-11T12:53:22.606883Z 0 [Warning] CA certificate ca.pem is self signed.
2018-02-11T12:53:22.608530Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2018-02-11T12:53:22.611190Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2018-02-11T12:53:22.611373Z 0 [Note] IPv6 is available.
2018-02-11T12:53:22.611457Z 0 [Note]   - '::' resolves to '::';
2018-02-11T12:53:22.611539Z 0 [Note] Server socket created on IP: '::'.
2018-02-11T12:53:22.620768Z 0 [ERROR] Can't start server : Bind on unix socket: Operation not permitted
2018-02-11T12:53:22.620942Z 0 [ERROR] Do you already have another mysqld server running on socket: /var/lib/mysql/mysql.sock ?
2018-02-11T12:53:22.621067Z 0 [ERROR] Aborting

2018-02-11T12:53:22.621204Z 0 [Note] Binlog end
2018-02-11T12:53:22.621421Z 0 [Note] Shutting down plugin 'ngram'
2018-02-11T12:53:22.621550Z 0 [Note] Shutting down plugin 'partition'
2018-02-11T12:53:22.621645Z 0 [Note] Shutting down plugin 'BLACKHOLE'
2018-02-11T12:53:22.621744Z 0 [Note] Shutting down plugin 'ARCHIVE'
2018-02-11T12:53:22.621832Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2018-02-11T12:53:22.621993Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
2018-02-11T12:53:22.622088Z 0 [Note] Shutting down plugin 'MyISAM'
2018-02-11T12:53:22.622199Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
2018-02-11T12:53:22.622266Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2018-02-11T12:53:22.622314Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2018-02-11T12:53:22.622378Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2018-02-11T12:53:22.622426Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2018-02-11T12:53:22.622488Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2018-02-11T12:53:22.622536Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2018-02-11T12:53:22.622599Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2018-02-11T12:53:22.622647Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2018-02-11T12:53:22.622724Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2018-02-11T12:53:22.622773Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2018-02-11T12:53:22.622837Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2018-02-11T12:53:22.622885Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2018-02-11T12:53:22.622947Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2018-02-11T12:53:22.623031Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2018-02-11T12:53:22.623087Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2018-02-11T12:53:22.623143Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
2018-02-11T12:53:22.623197Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
2018-02-11T12:53:22.623254Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2018-02-11T12:53:22.623308Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2018-02-11T12:53:22.623363Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2018-02-11T12:53:22.623418Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2018-02-11T12:53:22.623474Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2018-02-11T12:53:22.623528Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2018-02-11T12:53:22.623648Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
2018-02-11T12:53:22.623753Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2018-02-11T12:53:22.623845Z 0 [Note] Shutting down plugin 'INNODB_CMP'
2018-02-11T12:53:22.623953Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2018-02-11T12:53:22.624004Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
2018-02-11T12:53:22.624068Z 0 [Note] Shutting down plugin 'INNODB_TRX'
2018-02-11T12:53:22.624116Z 0 [Note] Shutting down plugin 'InnoDB'
2018-02-11T12:53:22.624273Z 0 [Note] InnoDB: FTS optimize thread exiting.
2018-02-11T12:53:22.624454Z 0 [Note] InnoDB: Starting shutdown...
2018-02-11T12:53:22.724955Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
2018-02-11T12:53:22.739902Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 180211 12:53:22
2018-02-11T12:53:24.348445Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2552072
2018-02-11T12:53:24.352699Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2018-02-11T12:53:24.353012Z 0 [Note] Shutting down plugin 'MEMORY'
2018-02-11T12:53:24.353110Z 0 [Note] Shutting down plugin 'CSV'
2018-02-11T12:53:24.353214Z 0 [Note] Shutting down plugin 'sha256_password'
2018-02-11T12:53:24.353308Z 0 [Note] Shutting down plugin 'mysql_native_password'
2018-02-11T12:53:24.353542Z 0 [Note] Shutting down plugin 'binlog'
2018-02-11T12:53:24.357751Z 0 [Note] mysqld: Shutdown complete

答案 2 :(得分:0)

您应该创建一个新类,例如MainMenu,你应该创建一个包含菜单所有路径的数组。 MainMenu的对象应返回带有标签和路径的数组,然后您可以将该数组传递给视图:

$menu = (new MainMenu())->buildMenu();
$response = $this->view->render($response, "index.phtml", [
    'menu' => $menu
]);

然后在您的*.phtml文件中,您可以访问$menu变量。但是,如果您不想在每条路线中重复该代码,该怎么办?

使用middlewares。您可以使用

从中间件传递变量
$request = $request->withAttribute('foo', 'bar');

并检索

$foo = $request->getAttribute('foo');