- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Zend Framework 2 的新手,我想学习这个框架。我想在路由器中创建 url 别名。例如,我在 module.config.php 中定义了这样的东西
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
'node' => array(
'type' => 'Application\Controller\AliasSegment',
'options' => array(
'route' => '/node[/:id]',
'constraints' => array(
'id' => '[0-9]+'
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
'id' => '0'
),
),
'may_terminate' => true,
),
),
),
当我键入 www.myapp.local/node/1
时,它会路由到我应用程序默认 Controller 中的默认操作。我想要的是一个可以处理 url 路径别名的路由器扩展。例如:
www.myapp.local/node/1 = www.myapp.local/aboutus
www.myapp.local/node/2 = www.myapp.local/company/gallery
我知道这在 ZF 是可能的。以下是如何在 ZF 中实现此目的的教程链接: friendly urls我知道这是波兰语,但我认为代码是不言自明的:)
想法是使用 url helper 来使用别名或普通段 (node/[:id]) 组装有效的 url
我已经在我的 Application\Controller 文件夹中创建了 AliasSegment 类,但它向我显示错误:
Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Application\Controller\AliasSegment' in C:\xampp\htdocs\industengine\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:450 Stack trace: #0
我的 AliasSegment 类(不完整):
<?php
namespace Zend\Mvc\Router\Http;
use Traversable;
use Zend\Mvc\Router\Exception;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\RequestInterface as Request;
class AliasSegment extends Segment
{
public function match(Request $request, $pathOffset = null)
{
}
}
我找了几个小时的答案,但我找不到任何东西。请至少告诉我我做错了什么,在哪里插入代码或者您知道更好的解决方案?
我不是在寻找现成的应用程序。我想学习一些东西,但如果您能详细告诉我答案,我将不胜感激:)
提前致谢,对不起我的英语:)
已编辑:
我的自定义路由器现在可以正常工作了。目前,别名是硬编码的,但它可以工作。
我的 AliasSegment 类现在看起来是:
<?php
namespace Application\Controller;
use Traversable;
use Zend\Mvc\Router\Exception;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Mvc\Router\Http;
class AliasSegment extends \Zend\Mvc\Router\Http\Segment
{
public function match(Request $request, $pathOffset = null)
{
$uri = $request->getUri();
$path = $uri->getPath();
//sample logic here
//for /about/gallery uri set node id to 1
//todo: get action, controller and module from navigation
if($path == '/about/gallery'){
$uri->setPath('/node/1');
$request->setUri($uri);
}
return parent::match($request, $pathOffset);
}
protected function buildPath(array $parts, array $mergedParams, $isOptional, $hasChild)
{
if(isset($mergedParams['link'])){
return $mergedParams['link'];
}
return parent::buildPath($parts, $mergedParams, $isOptional, $hasChild);
}
}
在这种情况下,/about/gallery
是 /node/1
的别名。两个地址都是正确的。 buildPath 函数正确返回别名路径。好吧,我希望这对某人有用:)
但是我想在 Zend_Navigation 中使用名为“link”的附加参数设置它。
我已经完成了我想要实现的目标的 50%,但是现在我无法从我的路由器获取 Zend_Navigation。我不知道如何通过它。我想应该是这样的:
$sm = $this->getServiceLocator();
$auth = $sm->get('Navigation');
它在我的 IndexController 中有效,但在我的 AliasSegment 中无效。我需要在带有“链接”参数的导航数组节点中找到。
编辑
我找到了解决方案。答案如下。
最佳答案
unable to fetch or create an instance for Application\Controller\AliasSegment
如果这是 Controller ,那么我希望在 module.config.php 中有:
'controllers' => array(
'invokables' => array(
'\Application\Controller\AliasSegment' => '\Application\Controller\AliasSegment',
)
),
你的类的命名空间看起来也有点奇怪:
namespace Zend\Mvc\Router\Http;
关于:
namespace Application\Controller;
关于alias - ZF2 : create url aliases in router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13317876/
当我像这样使用 zend CLI 创建表单时,会生成一个表单并将其添加到表单文件夹中。 zf create form MyForm 我有一个自定义类,我想以相同的方式为其创建一个自定义命令,并希望查看
我有一个以 user_id 作为主键的用户表和一个以 user_date_id 作为主键的 user_dates 表,user_id 链接到用户表以及 start_date 和 end_date 字段
我得到了这个代码 $select ->from(array("e" => "embarcacoes")) ->join(array("i
我遇到了 PHP 和 Zend Framework 的这个小问题。 我使用 apt-get install libzend-framework-php 安装了 ZF它还安装了二进制 zf.sh和 ze
我有一小段(x86)程序集,我试图弄清楚它的作用。 ... 6: 81 ec 00 01 00 00 sub $0x100, %esp c: 31 c9
我正在将 ZF 用于 MVC 应用程序,并且对我的代码应该如何构建感到非常困惑。 我有一个过程应用程序,它基本上是 1 个巨大的长文件,其中包含我希望我的应用程序执行的所有功能......例如:get
我的身份验证适配器如下所示 $authAdapter->setTableName('register') ->setIdentityColumn('u
我们最近开始使用 Doctrine 2.2 和 Zend Framework 2 的一部分,以努力改进组织、减少重复等。今天,我开始提出实现服务层的想法,以充当我们的 Controller 和 Doc
我是 phpunit 测试的新手。任何人都可以帮助我如何测试图像中下面的行。 到目前为止我的测试是: public function testCanSendEmail() { $formDat
我正在尝试写这个,用 zf select 查询但没有成功 SELECT * FROM `subscribers` WHERE id IN (Select subscriber_id From gs_r
我正在使用 Zend Framework 的 Zend_Db_Table从数据库中获取数据的类。 我想通过向表中添加一些内容来“优化”我从表中获取的每一行。在一个普通的旧 SQL 查询中,我会写例如。
我正在尝试使用 Zend_Db 进行与此类似的更新: UPDATE `TABLE` SET column = column + 'new value' WHERE foo = 'bar' 你们
cmpxchg 的操作伪代码如下(Intel® 64 and IA-32 Architectures Software Developer's Manual, Volume 2A: Instructi
我正在使用具有 REST API 的 ZF 开发应用程序。一切都很顺利,除了我的 XML 在开头有一个空白字符,所以 XML 打破了在开头有 XML 声明的规则。我正在尝试使用 javascript/
这段代码是否给了我最后插入的记录 id,即使在重载页面上? db = Zend_Db_Table::getDefaultAdapter(); $db->insert($this->_name,
为什么 lists:zf/2 是 Erlang 中 filtermap/2 的别名? 这是列表中未记录但已导出的函数:模块。这是实现: zf(F, L) -> filtermap(F, L).
我正在 x86 上调试我的代码,问题追溯到 AND 指令如果结果不为零,有时不会清除 ZF 标志。这是有问题的代码: 0257A9F9 mov edx,dword ptr [ecx+
我需要使用一些没有 GCC 内在函数的 x86 指令,例如 BSF 和 BSR。使用 GCC 内联汇编,我可以编写如下内容 __INTRIN_INLINE unsigned char bsf64(un
我在将复选框包装到标签中时遇到了 Zend 装饰器 (ZF1) 的一些问题。 在我的表格中,我有一些非常简单的东西: $remember = new Zend_Form_Element_Checkbo
我有一个简单的查询,但我一辈子也想不通。 我有一个包含“thread_id”和“messages”列的(简化的)“messages”表 我还有一个线程列表,每个线程需要 10 个结果。 $list =
我是一名优秀的程序员,十分优秀!