无法获得无法路由的HMVC请求以在FuelPHP中工作 [英] Can't get unroutable HMVC request to work in FuelPHP

查看:116
本文介绍了无法获得无法路由的HMVC请求以在FuelPHP中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在FuelPHP的文档中,它具有以下内容样本:

In the documentation of FuelPHP, it has the following sample:

// or fetch the output of a module
$widget = Request::forge('mymodule/mycontroller/mymethod/parms', false)->execute();
echo $widget;

这在我调用的函数具有action_前缀时有效,但是当我删除该前缀(因为我不希望浏览器调用该前缀)时,即使我设置了该前缀也不再起作用false的第二个参数.

This works when the function I am calling has the action_ prefix, but when I remove the prefix (since I don't want it to be called by the browser) it doesn't work anymore even if I set the 2nd parameter to false.

这里是一个例子:

作品

在一个控制器中,我打电话给

In one controller I call:

$widget = Request::forge('mymodule/mycontroller/mymethod')->execute();
echo $widget;

在mycontroller中:

In mycontroller:

public function action_mymethod()
{
    echo 'works';
}


失败404

在一个控制器中,我打电话给

In one controller I call:

$widget = Request::forge('mymodule/mycontroller/mymethod', false)->execute();
echo $widget;

在mycontroller中:

In mycontroller:

public function mymethod()
{
    echo 'works';
}

推荐答案

您无法删除操作前缀.

您不了解FuelPHP中的HMVC如何工作.从控制器的角度来看,无论请求来自何方,它都是一个请求.可以从URL(主要请求)或次要请求(HMVC)调用任何操作.

You don't understand how HMVC in FuelPHP works. From the controllers point of view, a request is a request, no matter where it comes from. Any action can be called either from the URL (the main request) or through secondary requests (HMVC).

Request :: forge()方法的第二个参数仅控制路由.如果为true(默认值),则请求是通过路由引擎发送的,因此,当将请求URI映射到控制器/方法时,路由将适用.如果为false,则绕过路由引擎,并直接映射到控制器/方法.

The second parameter of the Request::forge() method just controls the routing. If true (the default), the request is send through the routing engine, so routes will apply when mapping the request URI to a controller/method. If false, the routing engine is bypassed, and a direct mapping is made to the controller/method.

如果路由表的末尾包含一个包罗万象,则将需要此设置,以避免路由到内部控制器.这是防止控制器通过主请求被调用的首选方法.

You will need this if your routing table contains a catch-all at the end to avoid routing to internal controllers. This is the preferred way of shielding controllers from being called through a main request.

如果同时具有公共方法和内部方法的控制器,则由于需要从catch_all中排除某些URI,因此使用route选项可能会变得很复杂.

If you have controllers with both public and internal methods, using the route option can become complex as you need to exclude some URI's from the catch_all.

在这种情况下,您可以使用以下命令在控制器操作中检查请求类型:

In that case you can check the request type in the controller action using:

\Request::is_hmvc()

如果您的操作由主请求(即通过浏览器URL)调用,则返回false;如果是HMVC调用,则返回true.您可以重定向到其他地方,或者如果希望显示404,则抛出HttpNotFoundException.

This will return false if your action is called by the main request (i.e. via the browser URL) or true if it was an HMVC call. You can redirect elsewhere, or throw a HttpNotFoundException if you want your 404 to be shown.

这篇关于无法获得无法路由的HMVC请求以在FuelPHP中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆