dslh32311 2016-05-20 17:46
浏览 108
已采纳

CakePHP Rest API提供404输出:URL是否重写原因,以及如何修复它?

I am trying to create a Rest API in PHP, and I would like to use a framework. I have followed several tutorials on the subject. Despite my efforts, I am unable to get any of them to work, for either cakephp or codeigniter. The sites that contain these tutorials have numerous comments attached from people giving compliments to the writer. So I believe somebody got them to work.

Here is one example of what I have done: I built the Rest application according to this tutorial http://www.bravo-kernel.com/2015/04/how-to-build-a-cakephp-3-rest-api-in-minutes/ using composer to install my plugins and extensions. I did all of this under the directory path /var/www/html/cakephp/cake3api_app/. I built the cocktails table in my database, and I reconfigured config/app.php to have access to it. I opened up my FireFox web browser, and accessed the index page. Among other things, it had this output:

Please be aware that this page will not be shown if you turn off debug mode unless you replace src/Template/Pages/home.ctp with your own version.

    URL rewriting is not properly configured on your server.

    1) Help me configure it

    2) I don't / can't use URL rewriting
    Environment

    Your version of PHP is 5.5.9 or higher (detected 5.6.20-0+deb8u1).

    Your version of PHP has the mbstring extension loaded.

    Your version of PHP has the openssl extension loaded.

    Your version of PHP has the intl extension loaded.
    Filesystem

    Your tmp directory is writable.

    Your logs directory is writable.

    The FileEngine is being used for core caching. To change the config edit config/app.php
    Database

    CakePHP is able to connect to the database.
    DebugKit

    DebugKit is loaded.

I go to my Rest client and enter a local host URL that I can't post due to lack of reputation (localhost "slash" cakephp "slash" cake3api_app "slash" cocktails), and this is what it gave me:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title> 
</head><body>
<h1>Not Found</h1>
<p>The requested URL /cakephp/cake3api_app/cocktails was not found on this server.</p>
<hr>
<address>Apache/2.4.10 (Debian) Server at localhost Port 80</address>
</body></html>

I assume (perhaps incorrectly) this is because I need to configure my URL rewriting on Apache. I followed the URL given by the aforementioned CakePHP debug page (lack of reputation prevents me from posting the link), and it is all Greek to me. It seems I need to write what the page is talking about to an .htaccess file, but I am unsure which one, and also where in the .htaccess file I need to write it. There are several .htaccess files that come with CakePHP and the zipped tutorial code from GitHub has several of its own.

So my question is this: What do I need to do in order to make the code in question work in such a way that I can type a URL into my browser and it gives me JSON data in return?

The source code to the tutorial can be found here: https://github.com/bravo-kernel/application-examples/tree/master/blog-how-to-build-a-cakephp3-rest-api

Edit:

I changed the lines in /etc/apache2/apache2.conf that read "AllowOverride None" to read "AllowOverride All", but this had no effect.

Adding "LoadModule rewrite_module libexec/apache2/mod_rewrite.so" to /etc/apache2/apache2.conf broke apache, but adding it to the .htaccess at the CakePHP application root has no effect.

The file /etc/apache2/sites-available/default did not exist on my machine, but creating it and adding the code listed on the CakePHP book page had no effect.

There was not an app/ directory in the CakePHP application, but creating one and adding an .htaccess file to it with the contents specified in the CakePHP book had no effect.

Adding the line "RewriteBase /var/www/html/cakephp/cake3api_app/" to the .htaccess file in the webroot directory had no effect.

I restarted Apache after each of these changes.

  • 写回答

1条回答 默认 最新

  • doufanglian7585 2016-05-26 13:44
    关注

    URL rewriting is necessary to make the CakePHP framework work. This is how I solved the problem for myself. Please note that this solution worked for me on Debian Jessie running Apache version 2.4. It may not work for other versions of Linux and definitely won't work for Windows.

    First, I edited /etc/apache2/apache2.conf at about line 164 and changed the <Directory /var/www/> rule to read:

    <Directory /var/www/html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    

    Second, and this is important, shut down the computer and restart it. The reason why this is needed instead of restarting Apache is because Apache on Debian relies on a specific script Debian runs on start up to set environment variables, and using the binary Apache command to restart Apache will not set these variables. Rebooting the computer was good enough for me on a development machine.

    After taking these two steps, I was able to get the Rest API to work, and the CakePHP index page displayed background colors behind each section of messages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器