doutangqi4444 2015-10-14 13:40
浏览 92

php中的路由不起作用

Still quite new to php, please forgive my ignorance.

context

The server uses php slim as a submodule, i.e. this server is nested within some other folder which represents a larger application server (moodle to be exact). So the file dir would be as follows

/var/www/html/moodle/module/submodule/phpserver/api/index.php

problem / confusion

the base url works

$app->get(
    '/',
    function () {
        echo "basic test text";
    }
); 

Therefore this would return basic test text to the browser if I entered

localhost/moodle/module/submodule/phpserver/api/ 

But ANY other route returns a 404 e.g.

$app->get(
    '/Count_Schools',
    function () {
        echo "sample count";
    }
);

This returns a 404.

I'm not quite sure how the only the base url worked.

Failed resolutions

Explicitly setting the base url

$app->hook('slim.before', function () use ($app) {
    $app->view()->appendData(array('baseUrl' => '/localhost/moodle/report/api'));
});

setup

require 'Slim/Slim.php';
require 'db.php';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$app->hook('slim.before', function () use ($app) {
    $app->view()->appendData(array('baseUrl' => '/localhost/moodle/report/api'));
});

// GET route
$app->get(
    '/',
    function () {
        echo "basic test text";
    }
);
$app->get(
    '/Count_Schools',
    function () {
        echo "sample text";
    }
);

htaccess

# On some PHP servers it may help if this file is copied
# to the main moodle directory and renamed .htaccess
#
# As soon as you do this, check your web site.  Is it
# still working OK?  If you are getting a "configuration
# error" then you may need to enable overrides by editing
# the main httpd.conf for Apache and in the main server
# or virtual server area, adding something like:
#
# <Directory /web/moodle>
#     AllowOverride All
# </Directory>
#

### Firstly, if you are using Apache 2, you need the following
### three lines to allow Apache to pass a PATH_INFO variable
### correctly for URLs like http://server/file.php/arg1/arg2

<IfDefine APACHE2>
    AcceptPathInfo on
</IfDefine>

### Secondly, you can define the default files in the Moodle
### directories as follows:

DirectoryIndex index.php index.html index.htm

### Thirdly, set up some PHP variables that Moodle needs

php_flag file_uploads            1
php_flag short_open_tag          1
php_flag session.auto_start      0
php_flag session.bug_compat_warn 0

### Fourthly, sometimes Apache limits the size of uploaded files
### (this is a separate limit to the one in PHP, see below).
### The setting here turns off this limitation

LimitRequestBody 0


### These are optional - you may not want to override php.ini
### To enable them, remove the leading hash (#)

#php_value upload_max_filesize 2M
#php_value post_max_size 2M
#php_value session.gc_maxlifetime 7200


### You can change the following line to point to the
### error/index.php file in your Moodle distribution.
### It provides a form which emails you (the admin)
### about 404 errors (URL not found).

#ErrorDocument 404 http://example.org/moodle/error/index.php


### People have reported that these can help in some cases
### (unusual) when you see errors about undefined functions

#php_value auto_prepend_file none
#php_value include_path .

Note: I did not create an htaccess for my project, this is the htacceess for the larger project. as such it is kept in

/var/www/html/moodle/lib
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)