Yii2 如何将当前 Url 路由到当前项目根路径之外的控制器?
Yii2 How can I route the current Url to controller outside the root path of the current project?
我在 /var/www/ 目录中的主目录中有 2 个网站项目
我想要做的是创建控制器,它的视图在 2 个项目之间共享,而不是在两个项目上重复相同的控制器,
现在我创建它。 myController.php 在主机 A 内
如何从第二台主机B访问控制器?
并渲染我的动作功能?
main.php 配置文件中的 url 规则
'newpage/list'=>'myController/myaction',
编辑 :: 我正在使用这个高级模板
**DIRECTORY STRUCTURE**
common
config/ contains shared configurations
mail/ contains view files for e-mails
models/ contains model classes used in both backend and frontend
tests/ contains tests for common classes
console
config/ contains console configurations
controllers/ contains console controllers (commands)
migrations/ contains database migrations
models/ contains console-specific model classes
runtime/ contains files generated during runtime
backend
assets/ contains application assets such as JavaScript and CSS
config/ contains backend configurations
controllers/ contains Web controller classes
models/ contains backend-specific model classes
runtime/ contains files generated during runtime
tests/ contains tests for backend application
views/ contains view files for the Web application
web/ contains the entry script and Web resources
frontend
assets/ contains application assets such as JavaScript and CSS
config/ contains frontend configurations
controllers/ contains Web controller classes
models/ contains frontend-specific model classes
runtime/ contains files generated during runtime
tests/ contains tests for frontend application
views/ contains view files for the Web application
web/ contains the entry script and Web resources
widgets/ contains frontend widgets
vendor/ contains dependent 3rd-party packages
environments/ contains environment-based overrides
问题是:如何从后端规则访问前端目录中的前端控制器?
前端和后端是两个不同的模块。当它们从 index.php 引导时,它们的行为就像两个单独的项目。因此,您不能使用 Yii 的 urlManager
从前端路由到后端,反之亦然。
也许你可以在 common/params 中维护一些参数,在那里你可以配置绝对值 Url。
实际上您可以在后端重用前端控制器 类 - 您可以使用应用程序或模块的 controllerMap
属性 来定义自定义控制器 类。例如,如果您将这样的内容添加到您的后端配置中:
'controllerMap' => [
'mycontroller' => 'frontend\controllers\SomeController',
],
然后 frontend\controllers\SomeController
将表现得像 backend\controllers\MycontrollerController
- backend.local/mycontroller
将使用与 frontend.local/some
相同的控制器,但具有不同的上下文(可能还有布局)。
您甚至可以使用 controllerNamespace
从给定的命名空间加载所有控制器。例如在后端创建单独的模块:
namespace backend\modules;
class FrontendModule extends \yii\base\Module {
public $controllerNamespace = 'frontend\controllers';
}
然后该模块将在后端上下文中使用所有前端控制器。 backend.local/frontend/some
将使用 frontend\controllers\SomeController
.
我在 /var/www/ 目录中的主目录中有 2 个网站项目 我想要做的是创建控制器,它的视图在 2 个项目之间共享,而不是在两个项目上重复相同的控制器, 现在我创建它。 myController.php 在主机 A 内
如何从第二台主机B访问控制器? 并渲染我的动作功能?
main.php 配置文件中的 url 规则 'newpage/list'=>'myController/myaction',
编辑 :: 我正在使用这个高级模板
**DIRECTORY STRUCTURE**
common
config/ contains shared configurations
mail/ contains view files for e-mails
models/ contains model classes used in both backend and frontend
tests/ contains tests for common classes
console
config/ contains console configurations
controllers/ contains console controllers (commands)
migrations/ contains database migrations
models/ contains console-specific model classes
runtime/ contains files generated during runtime
backend
assets/ contains application assets such as JavaScript and CSS
config/ contains backend configurations
controllers/ contains Web controller classes
models/ contains backend-specific model classes
runtime/ contains files generated during runtime
tests/ contains tests for backend application
views/ contains view files for the Web application
web/ contains the entry script and Web resources
frontend
assets/ contains application assets such as JavaScript and CSS
config/ contains frontend configurations
controllers/ contains Web controller classes
models/ contains frontend-specific model classes
runtime/ contains files generated during runtime
tests/ contains tests for frontend application
views/ contains view files for the Web application
web/ contains the entry script and Web resources
widgets/ contains frontend widgets
vendor/ contains dependent 3rd-party packages
environments/ contains environment-based overrides
问题是:如何从后端规则访问前端目录中的前端控制器?
前端和后端是两个不同的模块。当它们从 index.php 引导时,它们的行为就像两个单独的项目。因此,您不能使用 Yii 的 urlManager
从前端路由到后端,反之亦然。
也许你可以在 common/params 中维护一些参数,在那里你可以配置绝对值 Url。
实际上您可以在后端重用前端控制器 类 - 您可以使用应用程序或模块的 controllerMap
属性 来定义自定义控制器 类。例如,如果您将这样的内容添加到您的后端配置中:
'controllerMap' => [
'mycontroller' => 'frontend\controllers\SomeController',
],
然后 frontend\controllers\SomeController
将表现得像 backend\controllers\MycontrollerController
- backend.local/mycontroller
将使用与 frontend.local/some
相同的控制器,但具有不同的上下文(可能还有布局)。
您甚至可以使用 controllerNamespace
从给定的命名空间加载所有控制器。例如在后端创建单独的模块:
namespace backend\modules;
class FrontendModule extends \yii\base\Module {
public $controllerNamespace = 'frontend\controllers';
}
然后该模块将在后端上下文中使用所有前端控制器。 backend.local/frontend/some
将使用 frontend\controllers\SomeController
.