我正在尝试创建一个路径(或两个),让我使用以下URL格式调用两个不同的操作.
mydomain.com/profile
mydomain.com/profile/1234/something
Run Code Online (Sandbox Code Playgroud)
对于第二种格式,1234
应该是必需的整数值,而something
应该是可选的字符串.通过使用文字路线,第一种格式很简单.我想我可以为第二种格式添加一个段子路由,但我无法让它工作.我试图省略第一种格式,只用分段路线做第二种格式,但我也没有成功.
这是我尝试过的:
'profile' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/profile',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'profile'
)
),
'child_routes' => array(
'profile_view' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:code[/:username]',
'constraints' => array(
'code' => '[0-9]*',
'username' => '[a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'view_profile'
)
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
因为mydomain.com/profile
,我得到以下错误:
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with …
Run Code Online (Sandbox Code Playgroud) php routing zend-route zend-framework2 zend-framework-routing
我刚开始学习zend-framework并遵循本用户指南.
我能够成功安装zend skeleton application
并转移到routing and controllers
.但是在完成本教程之后,我请求了网址:http://zf2-tutorial.localhost/album
在我的浏览器中,我得到了404
.
我瞧不起有些人在谈论变化的评论
'route' => '/album[/][:action][/:id]'
Run Code Online (Sandbox Code Playgroud)
需要是
'route' => '/album[/:action][/:id]'
Run Code Online (Sandbox Code Playgroud)
但这也无济于事.任何人都可以帮我解决这个问题吗?
目录结构:
这个url结构被提议用于SEO优化.因此建议另一种结构不起作用.建议的结构是
example.com/<language>/<country>/<province>/<city>/<product>
example.com/en/spain
我想指出CountryController
indexAction
,由于每个人的观点不同,有时候也认为布局也有变化.
显示关于国家西班牙的英语内容.对于请求example.com/en/india
应该用英语显示印度,并example.com/es/spain
应显示西班牙国家的西班牙语页面.
example.com/en/spain/barcelona
指着 CountryController
provinceAction
西班牙巴塞罗那省英语语言内容页面.
example.com/en/spain/barcelona/barcelona
指着 CountryController
cityAction
西班牙巴塞罗那省巴塞罗那市的英文内容页面.
example.com/en/spain/barcelona/barcelona/taxis
指着 CountryController
productAction
该产品的内容页面在巴塞罗那市,西班牙国家西班牙语的英语.
是的,我们可以添加一条路线
$router = $ctrl->getRouter();
$router->addRoute(
'country_spain',
new Zend_Controller_Router_Route('spain',
array('controller' => 'country',
'action' => 'index'))
);
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我们需要将整个国家列表添加到路线中.即印度,中国,巴基斯坦,联合国等.
然后将添加country_province
$router = $ctrl->getRouter();
$router->addRoute(
'country_spain_barcelona',
new Zend_Controller_Router_Route('spain',
array('controller' => 'country',
'action' => 'province'))
);
Run Code Online (Sandbox Code Playgroud)
因此,如果我们有50个省,那么增加国家数量乘以各国的省份数将是可怕的,这将成为转移到城市和产品时的更多路线.
你可以说添加类似的东西
$router = $ctrl->getRouter();
$router->addRoute(
'country',
new Zend_Controller_Router_Route('country/:country/:province/:city/:product',
array('controller' => 'country',
'action' => 'index'))
);
Run Code Online (Sandbox Code Playgroud)
但在这种情况下它就像我们将指向相同的动作,但随着请求的视图改变,这将成为一个胖控制器.
Zend_Controller_Router_Route_Regex的问题是我们应该有类似的东西
example.com/en/country/spain/barcelona/barcelona/taxis
以及所有移动到单个动作.由于视图完全不同,它变得很脏.可能是我可以使用的偏见.但我想知道是否有另一个好的解决方案可以解决这个问题.这是一个遗留项目,所以我对它有限制,#ZF版本是1.6.
有一个类似的例子 …
我在设置child_routes方面遇到了一些麻烦.除非我把它们分开,否则它们不起作用,最终结果应该是相同的!:
这就是我想要实现的目标:
'router' => array(
'routes' => array(
'app' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '[/:info]/app',
'defaults' => array(
'__NAMESPACE__' => 'X\App',
'controller' => 'Index',
'action' => 'index',
),
'may_terminate' => true,
'child_routes' => array(
'example' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/example[:/data]',
'defaults' => array(
'action' => 'example',
),
),
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
但它只能这样工作:
'router' => array(
'routes' => array(
'app' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array( …
Run Code Online (Sandbox Code Playgroud) 我需要动态更改我的语言应用程序。我有以下路线配置:
'route' => '/[:lang[/:controller[/:action[/:id]]]][[/page/:page]]',
'defaults' => array(
'lang' => 'en',
),
Run Code Online (Sandbox Code Playgroud)
是否可以从我的控制器或我的 Module.php(onBootstrap 函数)更改参数“lang”。我不知道我是否可以使用 globale 变量或类似的东西。
'defaults' => array(
'lang' => $my_variable,
),
Run Code Online (Sandbox Code Playgroud)
如果可能,我该如何改变它?
感谢您的帮助!
我正在创建一个控制器,它将从数据库中提取画家的信息并显示出来:PainterController
.我这样定义它:
<?php
class PainterController extends Zend_Controller_Action
{
public function init()
{
//setup painter route
$router = $this->getFrontController()->getRouter();
$router->addRoute(
'painter',
new Zend_Controller_Router_Route(
'painter/:id',
array(
'controller' => 'painter',
'action' => 'info'
)
)
);
}
public function indexAction()
{
//index
}
public function infoAction()
{
//info was requested for given ID
}
}
?>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,路由设置为接受类似于domain.com/painter/12
此示例中的id 12传递给infoAction的任何内容.
然而,当我访问这样的URL时,路由未被识别,而是我得到:
Message: Action "2" does not exist and was not trapped in __call()
Stack trace:
#0 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Controller/Action.php(515): Zend_Controller_Action->__call('2Action', Array)
#1 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('2Action') …
Run Code Online (Sandbox Code Playgroud) I have a problem using my url view helper. I have defined custom routes like so:
; Index
routes.domain.type = 'Zend_Controller_Router_Route_Static'
routes.domain.route = '/'
routes.domain.defaults.controller = index
routes.domain.defaults.action = index
Run Code Online (Sandbox Code Playgroud)
使用自定义网址时,一切正常,但我无法正常组合.我尝试使用视图中的以下代码添加链接:
$this->url(array('controller' => 'search', 'action' => 'index');
Run Code Online (Sandbox Code Playgroud)
问题是,当我在索引控制器的索引页面中使用此代码时,返回的url是当前控制器/操作的url,而不是我需要的url.
一些用户上传了存储在目录中的机密合同/协议文件/var/www/html/project/public/contract/<HERE_UNIQUE_FILES.pdf>
.
但问题是来自谷歌搜索或直接链接任何未经授权的用户可以打开它并查看/复制它.
我如何保护它,以便只有我的域或允许的对等方只能访问此私人目录?
例:
class Application_Model_Uploader
{
public static function mvUploadContract()
{
/* Anyone from outside can access this path, but how to protect it? */
$target_path = APPLICATION_PATH . "/../public/contract/";
$target_path = $target_path . basename( $_FILES['contractfile']['name']);
if(move_uploaded_file($_FILES['contractfile']['tmp_name'], $target_path))
{
$result = true;
}else{
$result = false;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个可以援引作为一个控制器modulename/xmlcoverage
与index
行动以及其他一些动作,让说testAction()
.这个控制器的网址是xml/coverage
.
然后,默认方式xml/coverage
映射到我的索引操作.并xml/coverage/test
映射到testAction.如果我需要testAction的id,那么url就像xml/coverage/test/33
是.
但是,对于索引操作,它需要是xml/coverage/index/33
我希望的位置xml/coverage/33
.
这是我的路线
'xmlcoverage' => array(
'type' => 'segment',
'options' => array(
'route' => '/xml/coverage[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'modulename/xmlcoverage',
'action' => 'index',
),
),
),
Run Code Online (Sandbox Code Playgroud)
在尝试url xml/coverage/33时,我认为33应该映射到id,因为它与action的正则表达式不匹配,并且两者都是可选的.由于它与动作不匹配,因此应使用默认(索引).
相反,我得到一个错误,说该网址无法匹配.所以对我来说,它的行为好像是'/xml/coverage[/:action[/:id]]'
因为我出于某种原因必须指定行动来识别id.
我做错了什么,我怎样才能让网址按照我的意愿行事?谢谢.
编辑:这是一个问题. 在我看来这样做:
$this->url('xmlcoverage', array('action' => 'index', 'id' => $someid))
Run Code Online (Sandbox Code Playgroud)
实际上在表单上提供了一个xml/coverage/1
会崩溃的URL !改变路线/xml/coverage[/:action[/:id]]
至少会使网址助手产生工作网址..