silex路由不起作用

时间:2015-11-03 01:30:14

标签: php routing openshift silex

我在我正在部署的silex应用程序上遇到路由问题。 我已经使用OpenShift成功部署了应用程序,您可以看到该网站here。 问题是当我尝试去除主页之外的任何页面时,我得到一个404错误,其中包含

  

未找到   在此服务器上找不到请求的URL /登录名。   Apache / 2.2.15(Red Hat)服务器位于f2f-face2face.rhcloud.com端口80

现在这让我相信我的app.php文件有问题,或者服务器正在尝试查找位于其他位置的文件。当我使用rhc“tail”日志工具时,我得到:

Mon Nov 02 19:28:46 2015] [error] [client 127.11.58.129] File does not exist: /var/lib/openshift/5637e0bd89f5cf939b00000e/app-root/runtime/repo/web/login, referer: http://f2f-face2face.rhcloud.com/ 98.246.242.195 - - [02/Nov/2015:19:28:46 -0500] "GET /login? HTTP/1.1" 404 295 "http://f2f-face2face.rhcloud.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"

我的app.php文件如下所示:

<?php
require_once __DIR__."/../vendor/autoload.php";
require_once __DIR__."/../src/User.php";
require_once __DIR__."/../src/Place.php";

$app = new Silex\Application();

// $app['debug']=true;

define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT',getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER',getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS',getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME',getenv('OPENSHIFT_GEAR_NAME'));


$server = 'mysql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT;;
$username = 'MYUSERNAMEATOPENSHIFT';
$password = 'MYPASSWORDATOPENSHIFT';
$DB = new PDO($server, DB_USER, DB_PASS);

$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/../views'
));

use Symfony\Component\HttpFoundation\Request;
Request::enableHttpMethodParameterOverride();

//home page (sign up page)
$app->get("/", function() use($app) {
    return $app['twig']->render('home.html.twig');
});

//after log off
$app->get("/logoff/{id}", function($id) use($app) {
    $user = User::find($id);
    $user->logOut();
    return $app['twig']->render('home.html.twig');
});

//list of users page
    //after sign up
$app->post("/signup", function() use($app) {
    $user_name = $_POST['user_name'];
    $password = $_POST['password'];
    $retype_password = $_POST['retype_password'];
    $latitude = 45.516231;
    $longitude = -122.682519;
    $signed_in = true;

    $user = new User($user_name, $password, $longitude, $latitude, $signed_in, $id=null);
    $user->save();

    return $app['twig']->render('users.html.twig', array('user' => $user, 'avialable_users' => $user->findUsersNear(), 'requests' => $user->findMeetupRequests()));
});

//log in page
$app->get("/login", function() use($app) {
    return $app['twig']->render('login.html.twig');
});

$app->get("/logged_on", function() use ($app) {
    $user_name = $_GET['username'];
    $user = User::findByUserName($user_name);
    $user_logged = $user->logIn($user_name, $_GET['password']);
    if($user_logged == "Wrong Password") {
        return $app['twig']->render('login.html.twig');
    } else {
        return $app['twig']->render('users.html.twig', array('user' => $user_logged, 'avialable_users' => $user->findUsersNear(), 'requests' => $user->findMeetupRequests()));
    }
});

$app->post("/request_meetup", function() use ($app) {
    $user1 = User::find($_POST['user1_id']);
    $user2 = User::find($_POST['user2_id']);
    $location = Place::setMeetupLocation($user1, $user2);
    $user1->addMeetUpRequest($user2->getId(), $location->getId());

    return $app['twig']->render('waiting_to_confirm.html.twig', array('user1_id' => $user1->getId(), 'user2_id' => $user2->getId()));
});

//waiting for request respond page
$app->get("/wait_for_confirmation", function() use ($app) {
    $user1 = User::find($_GET['user1_id']);
    $user2 = User::find($_GET['user2_id']);

    if(($user1->hasUserTwoConfirmed($user2->getId())) == NULL) {
        return $app['twig']->render('waiting_to_confirm.html.twig', array('user1_id' => $user1->getId(), 'user2_id' => $user2->getId()));
    } else {
        if(($user1->hasUserTwoConfirmed($user2->getId()))) {
            $location = Place::getMeetUpLocation($user1->getId(), $user2->getId());
            return $app['twig']->render('confirmed_user1.html.twig', array('user_to_meet' => $user2, 'user' => $user1, 'location' => $location));
        } else {
            return $app['twig']->render('rejected.html.twig', array('user' => $user1, 'user_to_meet' => $user2));
        }
    }
});
$app->post("/confirm_request", function() use ($app) {
    $user1 = User::find($_POST['user1_id']);
    $user2 = User::find($_POST['user2_id']);

    $user1->confirmMeetupRequest($user2->getId());
    $location = Place::getMeetUpLocation($user2->getId(), $user1->getId());
    return $app['twig']->render('confirmed_user2.html.twig', array('user_to_meet' => $user2, 'user' => $user1, 'location' => $location));
});

$app->post("/reject_request", function() use ($app) {
    $user1 = User::find($_POST['user1_id']);
    $user2 = User::find($_POST['user2_id']);

    $user1->rejectMeetupRequest($user2->getId());
    return $app['twig']->render('users.html.twig', array('user' => $user1, 'avialable_users' => $user1->findUsersNear(), 'requests' => $user1->findMeetupRequests()));
});

$app->post("/user1_confirm_meet", function() use ($app) {
    $user1 = User::find($_POST['user1_id']);
    $user2 = User::find($_POST['user2_id']);
    $user1->confirmMeetUserOne($user2->getId(), true);
    return $app['twig']->render('confirmation.html.twig', array('user' => $user1));
});

$app->post("/user2_confirm_meet", function() use ($app) {
    $user1 = User::find($_POST['user1_id']);
    $user2 = User::find($_POST['user2_id']);
    $user1->confirmMeetUserTwo($user2->getId(), true);
    return $app['twig']->render('confirmation.html.twig', array('user' => $user1));
});

$app->post("/user1_deny_meet", function() use ($app) {
    $user1 = User::find($_POST['user1_id']);
    $user2 = User::find($_POST['user2_id']);
    $user1->confirmMeetUserOne($user2->getId(), false);
    return $app['twig']->render('deny.html.twig', array('user' => $user1));
});

$app->post("/user2_deny_meet", function() use ($app) {
    $user1 = User::find($_POST['user1_id']);
    $user2 = User::find($_POST['user2_id']);
    $user1->confirmMeetUserTwo($user2->getId(), false);
    return $app['twig']->render('deny.html.twig', array('user' => $user1));
});

$app->get("go_home", function() use ($app) {
    $user = User::find($_GET['user1_id']);
    return $app['twig']->render('users.html.twig', array('user' => $user, 'avialable_users' => $user->findUsersNear(), 'requests' => $user->findMeetupRequests()));
});
return $app;

&GT?; 它是一长段代码,但你真正需要看到的是我使用/ login或/ signup这样的链接来路由到不同的页面。

编辑:我认为我可能需要.htaccess文件,但我也不确定并且无法找到实际需要进入该文件的内容。

我整天都被这一个难过,我真的很感激他的帮助。感谢。

1 个答案:

答案 0 :(得分:1)

位于docs

<IfModule mod_rewrite.c>
    Options -MultiViews

    RewriteEngine On
    #RewriteBase /path/to/app
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
</IfModule>

您基本上需要将所有请求重定向到不存在的文件到前端控制器。