popov/zfc-current

获取当前控制器、动作、路由、请求等信息的插件和助手

安装: 299

依赖项: 1

建议者: 0

安全: 0

星标: 3

关注者: 1

分支: 0

开放问题: 0

类型:zf-module

0.2.0 2018-04-04 14:57 UTC

This package is auto-updated.

Last update: 2024-09-07 02:43:20 UTC


README

#ZF2 Current Module 此插件允许通过控制器插件或视图助手获取当前命名空间、模块、控制器、动作、路由、路由器和请求。

安装

将包添加到您的composer.json中

{
    "require": {
        "popovsergiy/zfc-current": "dev-master"
    }
}

并更新您的供应商

$ php composer.phar update popovsergiy/zfc-current

别忘了将'Popov\Current'添加到config/application.config.php

return [
    'modules' => [
    	// ...
    	'Popov\Current'
    ]
]    

用法

此库具有控制器插件和视图助手,允许简单地访问主要的ZF2变量。

控制器用法

namespace YourModule\Controller

use Zend\Mvc\Controller\AbstractActionController;

class PostController extends AbstractActionController {

  public function indexAction() {
    $this->current('controller'); // post - controller name in module.config.php
    $this->current('action'); // index
    $this->current('module'); // YourModule  
    $this->current('route'); // RouteMatch object  
    $this->current('request'); // Request object
    
    $this->current()->currentModule(\Other\Module\Model\Entity::class); // Other\Module
  }
}

视图用法

// your-module/post/index.phtml

Current controller : <?= $this->current('controller') ?>
Current action : <?= $this->current('action') ?>
Current module : <?= $this->current('module') ?>

<?php
$action = $this->url('default/id', [
	'controller' => $this->current('route')->getParam('controller'),
	'action' => $this->current('route')->getParam('action'),
	'id' => $this->current('route')->getParam('id'),
]);
// or
$current = $this->current();
$action = $this->url('default/id', [
	'controller' => $current('route')->getParam('controller'),
	'action' => $current('route')->getParam('action'),
	'id' => $current('route')->getParam('id'),
]);
?>

技巧和提示

如果您使用转发插件并需要当前(转发)参数,下一个调用将返回预期的值

$this->current()->getController()->getEvent()->getRouteMatch()->getParam('action');

在其他所有情况下,您将获得真实的参数,这些参数位于URL中