CakePHP的和路由和谐AngularJs:htaccess的规则来发送用户的index.html或引导的index.php路由、规则、和谐、用户

2023-09-02 00:44:30 作者:小妞っ给你个机会说想我

我要建一个应用程序使用CakePHP作为我的后端和AngularJS为我的前端框架。

I'm building an app with CakePHP as my backend and AngularJS as my front-end framework.

我需要有CakePHP的同一台服务器作为我的前端code上,以便它可以成为我所需要的JSON。

I need to have CakePHP on the same server as my front-end code so it can serve the JSON I need.

通常在CakePHP中,一个人的根目录文件夹,除了JS / CSS /少/ *,有一个挂钩,CakePHP的一个index.php文件。添加AngularJS,虽然增加了一些复杂性,因为我也想要一个index.html。

Typically in CakePHP, one has the webroot folder, and besides the js/css/less/*, there is an index.php that hooks to CakePHP. Adding AngularJS, though, adds some complexity because I also want an index.html.

下面是控制流我想有:

如果用户进入domain.com/~~V,显示的index.html(但路由处于* .COM /'!) 如果用户进入domain.com/#/home~~V,显示index.html,然后让角照顾散路线 如果用户进入domain.com/about~~V(假设一下,被CakePHP的服务页面,HTML和所有),将其发送到index.php文件。 如果我做一个AJAX调用domain.com/users/list.json,使用的index.php这样的CakePHP可以服务于JSON。

所以,我想到的是我可以的.htaccess规则照顾这。但是我不太确定写什么。

So my thought was I could have .htaccess rules take care of this. But I'm not quite sure what to write.

现在我有:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d #if it's not an existing directory
    RewriteCond %{REQUEST_FILENAME} !-f #if it's not an existing file
    RewriteRule ^(.*)$ index.php?/$1 [QSA,L] #everything else, send to CakePHP
</IfModule>

任何想法?

这将是简单,只要最后一个条件,比如重写规则/ index.html的[L]

Would it be as simple as adding a line after the last condition like RewriteRule / index.html [L]?

此外,请随时提出使用CakePHP和AngularJS替代设置。

Also, please feel free to suggest alternative setups with CakePHP and AngularJS.

谢谢!

编辑:我想给构建系统的更多信息,我使用了AngularJS,为什么它带来的问题从home.ctp的观点只是服务

我用 NG-样板,它使用人缘用于测试和咕噜的串联,丑法,并index.html中包括脚本。

I'm using ng-boilerplate which uses karma for testing and grunt for concatenation, uglifying, and including scripts in index.html.

我觉得构建系统是真正的美丽,因为它可以让我保持一个模块化结构。

I think the build system is truly beautiful since it allows me to keep a modular structure.

下面是一个典型的 NG-样板前端的目录结构:

Here is the directory structure for a typical ng-boilerplate front-end:

ng-boilerplate/
  |- build/                    # Development build
  |  | index.html
  |  | assets/
  |  | 
  |  |- angular/
  |- grunt-tasks/
  |- karma/
  |- src/                      # Actual source code I'm editing
  |  |- app/
  |  |  |- <app logic>
  |  |- assets/
  |  |  |- <static files>
  |  |- common/
  |  |  |- <reusable code>
  |  |- less/
  |  |  |- main.less
  |- vendor/
  |  |- angular-bootstrap/
  |  |- bootstrap/
  |  |- placeholders/
  |- build.config.js           # Build configuration specific to app
  |- Gruntfile.js
  |- module.prefix
  |- module.suffix
  |- package.json

(在这些目录和文件夹,在这里的更多信息: https://github.com/joshdmiller/ NG-样板)

这使得一个困难的工作流程,因为我不得不采取最新的版本(发展中)和手动将index.html文件的 home.ctp 每次我编辑的index.html。

This makes for a difficult workflow, since I have to take the most recent 'build' (during development) and place the index.html file manually in home.ctp every time I edit index.html.

这就是为什么我的脑海里第一次去的.htaccess。不过,据我所知,这种想法有其自身的局限性,如果我曾经也想为来自CakePHP的一个页面,而不是直接将AngularJS进入画面。

This is why my mind first went to .htaccess. Though, I understand that this idea has its own limitations if I ever do want to serve a page from CakePHP directly rather than bringing AngularJS into the picture.

也许我可以配置构建系统放置的index.html在CakePHP的 home.ctp 视图。此解决方案的任何想法?

Maybe I could configure the build system to place the index.html in the CakePHP's home.ctp view. Any ideas on this solution?

推荐答案

一个蛋糕的主要原则是 KISS (保持简单(愚蠢))。

Keep it simple

One of Cake's main principles is KISS ("Keep it Simple (Stupid)").

您不需要任何重写规则变化,如果该URL的响应 / 是您的index.html文件的

You don't need any rewrite rule changes if the response for the url / is that of your index.html file

即。做此等价的:

cd app
mv webroot/index.html View/Pages/home.ctp
<edit View/Pages/home.ctp>

如果你把下面的视图文件的顶部:

If you put the following at the top of the view file:

<?php $this->layout = false; ?>

您还可以将内容完整的原始HTML文件。

You can also leave the contents as the complete raw html file.

这样做将不再需要任何的.htaccess 修改

Doing this would remove the need for any .htaccess modifications.

有关要点之一的具体注意事项:

A specific note about one of the bullet points:

如果用户进入domain.com/#/home~~V,显示index.html,然后让角照顾散路线

If the user goes to domain.com/#/home, display index.html and let Angular take care of the hash route

没有什么需要做的事情,作为一个URL片段是由浏览器与请求不发送 - 接收服务器上的URL是 domain.com /

Nothing needs doing for that, as a url fragment is not sent by browsers with requests - the url received on the server is domain.com/.

那么这就是所需的全部就是添加一条规则的网址 / - 这是唯一的例外:

Then all that's required is to add a rule for the url / - which is the only exception:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule  ^$ /index.html [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

请务必使用的.htaccess 规则蛋糕你使用,因为他们改变了多年来并可以使用的旧版本会导致异常行为的确切版本重写规则蛋糕的新版本。

Be sure to use the .htaccess rules for the exact version of cake you're using as they changed over the years and can cause odd behavior using an older version of the rewrite rule with a newer version of Cake.

注意,不需要目录排除条件,并很可能不希望(它允许目录中列出,例如 / CSS )。

Note that the directory-exclusion condition isn't required and is probably not desired (it allows a directory listing with e.g. /css).

 
精彩推荐
图片推荐