2016-02-29 32 views
1

我有一个在CakePHP 2.6上运行的AngularJS应用程序。我想在Angular中启用html5mode,所以我不必在URL中包含#,但这样做需要配置一些.htaccess文件,以便所有请求都能访问index.html。.htaccess和CakePHP:创建一个用于Angular-UI路由器的目录

并非所有我的web应用的是角,所以我不能只是重定向一切而不是index.html-,我想设置的东西,所以所有的请求一定子目录(及其子目录)重定向到该子目录的index.html

CakePHP有一个文件夹结构是这样的:

/     // Server web root directory: index.php, etc 
----/app   // The directory containing the actual Cake app 
    ----/webroot // Web assets used by the Cake app 
     ----/myapp // Where I'd like to store the index.html for my Angular app 

我希望得到这个设置,以便于/myapp或其子目录中的任何要求都会自动重定向到/app/webroot/index.html。有没有人有关于如何去做这件事的任何想法?

我现在的.htaccess文件:

/:

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteBase/
    RewriteRule ^$ app/webroot/ [L] 
    RewriteRule (.*) app/webroot/$1 [L] 
</IfModule> 

/应用:

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteRule ^$ webroot/ [L] 
    RewriteRule (.*) webroot/$1 [L] 
</IfModule> 

/应用程序/ webroot的:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_URI} !^/(app/webroot/)?(img|css|js|eot)/(.*)$ 
    RewriteRule ^(.*)$ index.php [QSA,L] 
</IfModule> 

回答

1

我只是在CakePHP中创建一个控制器来处理所有myapp的请求,并将静态HTML呈现为具有自定义布局(如果需要)的视图。

性能不会成为问题,因为CakePHP的路由只能执行一次。之后,AngularJS应用程序正在运行,并将在浏览器中本地接管所有HTML5路由。

尽管您可以在.htaccess中实现所需,但它限制了您在Apache上运行CakePHP应用程序。如果您想稍后扩展到IIS或Nginx,则必须重新创建这些重写规则。