2015-11-03 167 views
1

我; m在我正在部署的一個silex應用上路由時出現問題。 我已經使用OpenShift成功部署了該應用程序,您可以看到該網站here。 問題是,當我嘗試去到任何網頁,除了主頁我得到一個404錯誤讀取silex路由不能正常工作

未找到 所請求的URL /登陸此服務器上找到。 的Apache/2.2.15(紅帽)在f2f-face2face.rhcloud.com端口80

服務器現在這使我相信,我的app.php文件有什麼不對的地方,或者服務器正在嘗試找到位於其他位置的文件。當我使用RHC「尾巴」日誌工具,我得到:

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; 

> 它的碼長塊,但你?真的需要看到我正在使用像/ login或/ signup這樣的鏈接來路由到不同的頁面。

編輯:也我想我可能需要一個.htaccess文件,但我也不確定和無法找到實際需要進入該文件。

我一直在這一整天難住,我真的很感謝幫助。謝謝。

回答

1

它是docs

<IfModule mod_rewrite.c> 
    Options -MultiViews 

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

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

+0

真棒。所以我只是把這個文件放到我的web文件夾(我的文檔根目錄)中,並且一切似乎都正常。非常感謝我很難追蹤這一切如何運作! –