2015-07-12 18 views
0

我在一個PHP應用程序中使用超薄微架構

這是我的index.php文件的工作:

<?php 

require 'Slim/Slim.php'; 
include 'db.php'; 

\Slim\Slim::registerAutoloader(); 

$app = new \Slim\Slim(); 

$app->get("/", function() { 
    echo "<h1>HELLO USER</h1>"; 
}); 

    //Registration view 

    $app->post("/reg", function(){ 

     $request = $this->app->request(); 
     $username = $request->post('username'); 
     $password = $request->post('password');   
     $name = $request->post('name'); 
     $email = $request->post('email'); 
     try { 
      $sql = "INSERT INTO users (username, hash, name, email) VALUES (:username, :password, :name, :email)"; 
      $s = $this->dbh->prepare($sql); 
      $s->bindParam("username", $username); 
      $s->bindParam("hash", $password);    
      $s->bindParam("name", $name); 
      $s->bindParam("email", $email); 
      $s->execute(); 
     } catch(\PDOException $e) { 
      echo 'Exception: ' . $e->getMessage(); 
     } 

    }); // Login Function End 



$app->get('/updates', function() { 
    //Display users 

}); 


// run the Slim app 

$app->run(); 

?> 

和我有使用Ajax跨域請求index.html文件,像這樣的:

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>INDEX</title> 
<link href='css/style.css' rel='stylesheet' type='text/css'/> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<script> 


$(document).ready(function(){ 
    $("#put").submit(function(event) { 
     event.preventDefault(); 
     $.ajax({ 
      url: 'http://localhost/slim/reg', 
      type: 'POST', 
      crossDomain: true, 
      data: $("#put").serializeArray(), 
      success: function(data) {console.log(data); } 
     }); 
    }); 
}); 



</script> 

</head> 

<body> 

<form id="put"> 
    Registration <br/> 
    username: <input type="text" name="username" id="username"/><br /> 
    password: <input type="password" name="hash" /><br /> 
    name: <input type="text" name="name" /><br /> 
    email: <input type="text" name="email" /><br /> 
    <input type="submit" /> 
</form> 


</body> 
</html> 

的問題是,當我嘗試提交表單在我的index.html文件我總是得到:

Fatal error: Using $this when not in object context in C:\wamp\www\slim\index.php on line 19

+0

你嘗試刪除'$本 - >' –

+0

我做了19行是這樣的:$請求= $ app-> request(); 我在控制檯出現錯誤: 500內部服務器錯誤 – YoRk404

回答

1
$request = $this->app->request(); 

這是最有可能的線#19,這不是一個實例方法裏面,甚至沒有在定義類。所以,沒有實例參考,因此沒有$ this。
你期望這$是什麼?顯然你需要一個app和一個dbh。什麼是提供這些屬性/實例?可以是$app = new \Slim\Slim();。但是dbh應該是什麼? (數據庫句柄,好的,但是提供這個數據庫連接句柄是什麼?)

1

基本上問題是@VolkerK提到的。

如果您希望您的關閉方法知道您的方法範圍之外定義的變量,那麼您需要使用use關鍵字。

$app->post("/reg", function() use ($app) { 
    $request = $app->request(); 
} 
0

解決方案: 完整的工作,現在的index.php樣子:

<?php 


require 'Slim/Slim.php'; 
include 'db.php'; 

\Slim\Slim::registerAutoloader(); 

$app = new \Slim\Slim(); 
$app->contentType('application/json'); 

$app->get('/', 'home'); 
$app->post('/adduser', 'addUser'); 
$app->get('/users', 'getUsers'); 

// Home view 

function home() {echo "Welcome to the platform !";} 

// Add users 
function addUser() { 
    global $app; 
    $req = $app->request(); 

    $sql = "INSERT INTO users (username, password, fullname, email) VALUES (:username, :password, :fullname, :email)"; 
    try { 
    $db = getDB(); 
    $stmt = $db->prepare($sql); 
     $stmt->bindParam("username", $_POST['username']); 
     $password = md5($_POST['password']); 
     $stmt->bindParam("password", $password); 
     $stmt->bindParam("fullname", $_POST['fullname']); 
     $stmt->bindParam("email", $_POST['email']); 
     $stmt->execute(); 
     $db = null; 
    } catch(PDOException $e) { 
     echo "Connection Error"; 
    } 
} 

// Get Users 

function getUsers() { 
    $sql = "SELECT username,fullname,email FROM users ORDER BY id"; 
    try { 
     $db = getDB(); 
     $stmt = $db->query($sql); 
     $users = $stmt->fetchAll(PDO::FETCH_OBJ); 
     $db = null; 
     echo '{"users": ' . json_encode($users) . '}'; 
    } catch(PDOException $e) { 
     //error_log($e->getMessage(), 3, '/var/tmp/php.log'); 
     echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    } 
} 

// run the Slim app 

$app->run(); 

?> 
+1

我真的會盡量避免使用'global'。 – slik

相關問題