网址无法在php

时间:2018-10-17 18:11:38

标签: php api slim

我一直在遵循本教程Tutorial,并设法用slim创建了其余的api。

这是我的.htaccess

    RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

就像在示例中一样,然后当我进入index.php

http://localhost/thefaith/v1/user/register

我遇到错误

    404 Page Not Found

The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.

但是我所有的文件都整理得很好

下面是我的index.php,它位于v1文件夹中

    <?php
include_once '../Includes/account_db_handler.php'; 
require '.././libs/Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();

// User register
$app->post('/user/register', function() use ($app) {
    // check for required params
    //verifyRequiredParams(array('name', 'email'));

    $response = array();

    // reading post params
    //$name = $app->request->post('name');
    //$email = $app->request->post('email');

    // validating email address
    //validateEmail($email);

    //$db = new AcoountDbHandler();
    //$response = $db->createUser($name, $email);

    $response["message"] = "You are successfully registered";
    // echo json response
    echoRespnse(201, $response);

});


/**
 * Verifying required params posted or not
 */
function verifyRequiredParams($required_fields) {
    $error = false;
    $error_fields = "";
    $request_params = array();
    $request_params = $_REQUEST;
    // Handling PUT request params
    if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
        $app = \Slim\Slim::getInstance();
        parse_str($app->request()->getBody(), $request_params);
    }
    foreach ($required_fields as $field) {
        if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
            $error = true;
            $error_fields .= $field . ', ';
        }
    }

    if ($error) {
        // Required field(s) are missing or empty
        // echo error json and stop the app
        $response = array();
        $app = \Slim\Slim::getInstance();
        $response["error"] = true;
        $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
        echoRespnse(400, $response);
        $app->stop();
    }
}


function echoRespnse($status_code, $response) {
    $app = \Slim\Slim::getInstance();
    // Http response code
    $app->status($status_code);

    // setting response content type to json
    $app->contentType('application/json');

    echo json_encode($response);
}

/**
 * Validating email address
 */
function validateEmail($email) {
    $app = \Slim\Slim::getInstance();
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $response["error"] = true;
        $response["message"] = 'Email address is not valid';
        echoRespnse(400, $response);
        $app->stop();
    }
}


$app->run();

?>

还是苗条的新手,需要一些解决方法上的帮助

0 个答案:

没有答案