Yii2如何将当前的URL路由到当前项目的根路径之外的控制器?

时间:2018-07-04 20:16:27

标签: php url yii yii2 url-routing

我在/ var / www /目录中的主目录上有2个网站项目 我想要做的是创建一个控制器,使其视图在两个项目之间共享,而不是在两个项目上都重复相同的控制器, 现在我创建它。主机A内的myController.php

如何从第二台主机B访问控制器? 并呈现myaction函数?

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

问题是::如何从后端规则访问前端目录中的前端控制器?

2 个答案:

答案 0 :(得分:2)

前端和后端是两个不同的模块。当它们从index.php启动时,它们的行为就像两个单独的项目。因此,您无法使用Yii的set.seed(11) myv = letters[1:5] irand = sample(length(myv), length(myv)) # to randomly select partner1 allpairs = expand.grid(myv, myv) # remove pairs of the same item allpairs = allpairs[allpairs[,1]!=allpairs[,2],] usedpartner2 = c() # to store the partner2 which are already used mypairs = c() # to store results for (i in 1:length(myv)) { partner1 = myv[irand[i]] # the potential partner2 must be different from partner1 and not already used candidates = allpairs[allpairs[, 1]==partner1 & !(allpairs[, 2] %in% usedpartner2), 2] partner2 = as.character(candidates[sample(length(candidates), 1)]) usedpartner2 = c(usedpartner2, partner2) mypairs = rbind(mypairs, c(partner1, partner2)) } mypairs # [,1] [,2] # [1,] "b" "e" # [2,] "a" "b" # [3,] "e" "a" # [4,] "d" "c" # [5,] "c" "d" 从前端路由到后端,反之亦然。

也许您可以在common / params中维护一些参数,您可以在其中配置绝对URL。

答案 1 :(得分:0)

实际上,您可以在后端重用前端控制器类-您可以使用应用程序或模块的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