htaccess自己的phpmvc框架


htaccess for own php mvc framework

早上好,我正在构建自己的php框架。我希望除了对js-css.jpeg、jpg、png文件的请求外,所有请求都传递到index.php文件。因此,我希望一个独立于其他模块的模块拥有自己的资产文件夹,其中包含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

我该怎么做?

更新

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

而不是长

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

您可以将所有内容重写到index.php,排除某些文件夹的内容,例如:

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

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

实际上,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]

这是Kohana的访问示例:

# 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.');

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

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

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