【问题标题】:htaccess for own php mvc frameworkhtaccess 用于自己的 php mvc 框架
【发布时间】:2012-03-18 19:33:40
【问题描述】:

早上好, 我正在构建自己的 php 框架。我希望所有请求都传递给 index.php 文件,但对 js css jpeg、jpg、png 文件的请求除外。所以我希望一个独立于其他人的模块有自己的资产文件夹,其中包含 css 图像和 js 文件。

结构类似:

application
   module1
     models
     controllers
     templates
         assets
             css
             images
               test.jpg
               hello.png
             js

         view1.html.php
         view2.html.php

   module2
     models
     controllers
     templates
         assets
             css
             images
               test.jpg
               hello.png
             js

         view1.html.php
         view2.html.php
  core
    here is core file framework

  index.php

我该怎么做?

更新

我的模块在应用程序文件夹下。 assets 文件夹必须是公共的。 我想要一个简单的小网址,例如 www.example.com/module1/images/test.jpg

而不是长

www.example.com/application/module1/templates/assets/images/test.jpg

【问题讨论】:

    标签: php apache .htaccess url-rewriting


    【解决方案1】:

    您可以将所有内容重写为 index.php,不包括某些文件夹的内容,例如:

    RewriteCond %{REQUEST_URI} !^/(css|images|js)/
    RewriteRule .* index.php [L]
    

    /css、/images、/js文件夹下的所有文件都不会被重写。

    【讨论】:

      【解决方案2】:

      其实zf默认的.htaccess就可以很好的解决这个问题

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [NC,L]
      RewriteRule ^.*$ index.php [NC,L]
      

      【讨论】:

        【解决方案3】:

        这是来自Kohanaexample.htaccess

        # Turn on URL rewriting
        RewriteEngine On
        
        # Installation directory
        RewriteBase /
        
        # Protect hidden files from being viewed
        <Files .*>
            Order Deny,Allow
            Deny From All
        </Files>
        
        # Protect application and system files from being viewed
        RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
        
        # Allow any files or directories that exist to be displayed directly
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        
        # Rewrite all other URLs to index.php/URL
        RewriteRule .* index.php/$0 [PT]
        

        如果您只是删除有关保护应用程序和系统文件的行,应该可以使用。相反,只需确保这些文件不会自行执行任何操作。在 Kohana 中,通过在 index.php 中定义一个常量来解决,然后在所有不应从外部访问的文件的开头检查该常量。

        <?php defined('SYSPATH') or die('No direct script access.');
        

        【讨论】:

        • 我现在在 www.example.com/application/module1/templates/assets/images/test.jpg 上评论“重写规则保护应用程序”行我可以看到图像。为了直接访问核心文件框架和其他文件,我可以将常量控制作为 Kohana。我怎样才能有一个更简单的 url /module1/images/test.jpg 而不是所有 /application/module1/templates/assets/images/test.jpg ? ——
        • @NicolaPaganotti 也许移动文件?如果您不将其用于任何用途,则无需将其全部放在应用程序目录中...?
        • 我可能还会问自己,我做这一切是为了什么……为什么不从使用 Kohana 或不同的 MVC 框架开始呢?或者至少,如果它们属于同一个应用程序,是什么让你的这些模块如此独立?
        【解决方案4】:

        我最近开始研究自己的框架,发现下面的网站非常有用。它涵盖了 .htaccess 和设置基本 MVC 框架的所有其他方面。

        http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/

        我已经为 PDO 重写了所有数据库内容,调整了默认路由并更改了一些功能,但发现它非常有用,当然值得一读。

        【讨论】:

          猜你喜欢
          • 2011-04-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多