gpt4 book ai didi

angularjs - Yii2 + AngularJS

转载 作者:行者123 更新时间:2023-12-02 20:08:08 26 4
gpt4 key购买 nike

我在集成 Angular 和 Yii2 时遇到问题我有两部分:

  1. 常规 Yii2 应用
  2. 以 actionIndex 开头并与 Angular 一起使用的 Controller

问题是,当我在 Angular 部分复制并粘贴 url 时,我总是收到 404 错误。 Angular 配置是

.config(function($routeProvider, $locationProvider) {
base = '/construct/';
$routeProvider.when(base+':className', {
templateUrl: '/construct/builder',
controller: 'BuilderCtrl',
reloadOnSearch: false
}).otherwise({
templateUrl: '/construct/heroes',
controller: 'HeroesCtrl'
});

$locationProvider.html5Mode(true);
});

那么,我怎样才能使 HTML5 模式下的直接链接能够看到正确的页面呢?像“/construct/something”这样的路径不起作用。我的 htaccess 是:

RewriteEngine on

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php

最佳答案

htaccess 文件看起来正确,但如果您的 Web 服务器的 DocumentRoot 和 Web 应用程序位于不同的文件夹中,则需要首先在 htaccess 文件中设置 RewriteBase 路径,如果使用基于 UNIX 的操作系统,则如下所示:

RewriteEngine on

RewriteBase /~userName/pathToYourYiiProject/web

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php

如果您在从导航器手动访问路径时没有得到结果,那么您需要首先检查是否激活了漂亮的网址:

'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],

(查看 Mihai 的 yii 官方文档链接)

还要检查您的 apache 配置中是否激活了 rewrite_module。要在 Linux 操作系统中启用并加载 mod_rewrite,您必须启动这些脚本:

$ cat /etc/apache2/mods-available/rewrite.load
$ sudo a2enmod rewrite
$ ls -al /etc/apache2/mods-enabled/rewrite.load

最后记住,最近的 Apache 版本(从 2.3.9 开始)默认情况下具有“AllowOverride None”,您必须在/etc/apache2/apache2.conf 中将其更改为“AllowOverride All”(同样,Linux 操作系统中的路径不知道在其他操作系统中是否相同)。重新启动你的 apache 服务器并手动重新检查导航器中的路径,如果你得到 json 格式的结果,那么你的后端工作正常并且 angularJs 应该能够与其通信。

关于angularjs - Yii2 + AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27763649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com