在MVC中使用第三方javascript

时间:2014-05-09 09:37:20

标签: javascript php .htaccess mod-rewrite

我正在尝试调用位于〜/ vendor /目录中的javascript函数(请参阅下面的架构),但没有成功。如果我将javascript移动到我的目录〜/ public / js /,则调用成功。

我相信我的问题可能来自我的.htaccess文件,但我没有找到解决方法,尽管有一些读数。如果有人能指出我正确的方向,我会非常高兴。

这是目录结构(请注意root和./中的.htaccess):

<project name>/
    application/
        config/
        controllers/
        models/
        views/
    public/
        css/
        images/
        js/     <- if placed here, the call succeeds
        .htaccess
        index.php
    vendor/
        <third-party code>/
            javascript.js     <- here, it doesn't
    .htaccess

以下是.htaccess文件的内容:

    根目录上的
  • <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^$    public/    [L]
        RewriteRule    (.*) public/$1    [L]
     </IfModule>
    
  • 公共目录中的
  • <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?page=$1 [PT,L]
    </IfModule>
    

3 个答案:

答案 0 :(得分:1)

我建议要么:

public/js/Vendor
public/css/Vendor

public/Vendor/js
public/Vendor/css

将是最好的方式。我认为当你可以将Vendor文件夹添加到public(这是我认为它应该去的地方)时重写规则是不好的做法。

答案 1 :(得分:0)

应该位于公共目录之外的唯一代码是应用程序级代码,看起来就是PHP。

您的服务器加载的所有其他资产都应该存在于公共目录中,这就是为什么它被称为公共目录,因为公众可以通过您的网络服务器请求访问它。

另一种方法是在项目的根目录下创建一个js-source目录,并让像Grunt或Gulp这样的任务管理器编译并将js复制到你的公共目录中,如下所示:

project-name/
  application/
    config/
    controllers/
    models/
    views/
  public/
    css/
    images/
    js/     <- is filled with js from js-source/ by grunt/gulp
    .htaccess
    index.php
  js-source/
    vendor/
    main.js  <- is uglified/concatinated & copied into public/js by grunt/gulp
.htaccess

参考链接:

答案 2 :(得分:0)

您可以将htaccess从公共库复制到供应商库,以便它覆盖您的根目录htaccess。

相关问题