简体   繁体   中英

Slim Framework - 404 Page Not

I came across Slim framework and have been playing with it. I'm running into a 404 issue. With the code below: I know this has something to do with the url but I'm not quite sure how to fix this problem. Do I have to define the url for "profiles" somewhere?

also my directories look like this:

include

  • dbhandler.php

  • config.php

  • dbConnect.php

lib

  • Slim

v1

  • .htaccess
  • index.php

Problem in this code: mysite.com//parentfolder/v1/profiles throws 404 error

$app->post('/profiles', 'authenticate', function() use ($app) {
            // check for required params
            verifyRequiredParams(array('gender'));

            $response = array();
            $gender = $app->request->post('gender');

            global $user_id;
            $db = new DbHandler();

            // creating new task
            $profile_id = $db->createUserProfile($user_id, $gender);

            if ($profile_id != NULL) {
                $response["error"] = false;
                $response["message"] = "Profile created successfully";
                $response["profile_id"] = $profile_id;
                echoRespnse(201, $response);
            } else {
                $response["error"] = true;
                $response["message"] = "Failed to create profile. Please try again";
                echoRespnse(200, $response);
            }            
});

.htaccess

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

I also have this function that works just fine.

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

            $response = array();

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

            // validating email address
            validateEmail($email);

            $db = new DbHandler();
            $res = $db->createUser($name, $email, $password);

            if ($res == USER_CREATED_SUCCESSFULLY) {
                $response["error"] = false;
                $response["message"] = "You are successfully registered";
            } else if ($res == USER_CREATE_FAILED) {
                $response["error"] = true;
                $response["message"] = "Oops! An error occurred while registereing";
            } else if ($res == USER_ALREADY_EXISTED) {
                $response["error"] = true;
                $response["message"] = "Sorry, this email already existed";
            }
            // echo json response
            echoRespnse(201, $response);
        });

Assuming /parentfolder/v1/ is the physical folder where your code lives. Add the following to your .htaccess .

RewriteBase /parentfolder/v1/

I am not sure where the ENV:BASE comes from. Unless you are sure you must use it, use rewrite rules as instructed in Slim documentation.

RewriteEngine On
RewriteBase /parentfolder/v1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM