2015-10-17 33 views
1

我不知道我該如何處理Slim中的錯誤。如何管理我的Slim應用程序中的錯誤?

我正在使用具有函數的類,例如登錄。當用戶輸入不正確的密碼時,我不確定如何將它們提供給Slim,以便它們能夠顯示給用戶。

這是我目前有:

路線:

$app->post('/submit', function() use ($app) { 
    $post = (object) $app->request->post(); 

    if($app->user->login($post->log_mail, $post->log_pass)){ 
     $app->setCookie('mail', $post->log_mail, '2 minutes'); 
     $app->redirect('/inbox'); 
    }else{ 
     // redirect 
     // display errors set by the $app->user->login() function. 
    } 
}); 

類功能:

public function login($username, $password){ 
    if(!empty($username)){ 
     if(!empty($password)){ 

      if($this->numrows($username, $password)){ 

       // login success 
       return true; 


      }else{die('credentials');} 

     }else{die('password');} 
    }else{die('username');} 
} 

我如何使用Cookie的影響:

$app = new \Slim\Slim(
[ 
    'debug' => true, 
    'mode' => 'development', 
    'cookies.encrypt' => true, 
    'cookies.secret_key' => 'supersecret', 
]); 
$app->setCookie('something', 'something' '2 minutes'); 

我在哪裏我目前正在使用die(),我想設置一個實際的錯誤,以便Slim視圖能夠呈現它。我看着Slim的Flash功能,但到目前爲止沒有工作。

+0

什麼版本的修身您使用的是? –

+0

Slim 2我相信,我注意到,現在有第三個,但還沒有更新 – Pepzter9

回答

1

UPDATE

documentation

如果使用會話cookie中間件,你並不需要啓動 本地的PHP會議。

使用\Slim\Middleware\SessionCookie,代碼爲:

$app = new Slim(); 
$app->add(new \Slim\Middleware\SessionCookie(array(
    'expires' => '20 minutes', 
    'path' => '/', 
    'domain' => null, 
    'secure' => false, 
    'httponly' => false, 
    'name' => 'slim_session', 
    'secret' => 'CHANGE_ME', 
    'cipher' => MCRYPT_RIJNDAEL_256, 
    'cipher_mode' => MCRYPT_MODE_CBC 
))); 

這是我工作的解決方案,而無需使用\Slim\Middleware\SessionCookie

指數。PHP

<?php 
require 'vendor/autoload.php'; 
session_start(); 

// Twig 
$twigView = new \Slim\Views\Twig(); 

$twigView->parserExtensions = array(
    new \Slim\Views\TwigExtension() 
); 

$app = new \Slim\Slim(
[ 
    'debug' => true, 
    'mode' => 'development', 
    'cookies.encrypt' => true, 
    'cookies.secret_key' => 'supersecret', 
    'view' => $twigView 
]); 
$app->setCookie('something', 'something', '2 minutes'); 

class User { 

    private function numRows($username, $password){ 
     //Your magic here 
     return true; 
    } 

    public function login($username, $password){ 
     $app = \Slim\Slim::getInstance(); 
     if(!empty($username)){ 
      if(!empty($password)){ 

       if($this->numrows($username, $password)){ 
        // login success 
        return true; 
       }else{ 
        $app->flash('error', 'credentials'); 
       } 

      } else{ 
       $app->flash('error', 'password'); 
      } 
     } else{ 
      $app->flash('error', 'username'); 
     } 
     return false; 
    } 
} 

//Inject the new user 
$app->user = new User(); 

$app->get('/', function() use ($app){ 
    $flash = $app->view()->getData('flash'); 
    $app->render('form.html'); 
})->name('Login'); 

$app->get('/inbox', function() use ($app){ 
    echo "Login OK"; 
})->name('Inbox'); 


$app->post('/submit', function() use ($app) { 
    $post = (object) $app->request->post(); 
    if($app->user->login($post->log_mail, $post->log_pass)){ 
     $app->setCookie('mail', $post->log_mail, '2 minutes'); 
     $app->redirect($app->urlFor('Inbox')); 
    }else{ 
     // redirect 
     // display errors set by the $app->user->login() function. 
     $app->redirect($app->urlFor('Login')); 
    } 
}); 

$app->run(); 

form.html

<html> 
<head> 
    <title>Stackoverflow 33192640</title> 
</head> 

<body> 
    <form action="./submit" method="POST"> 
     <input type="text" name="log_mail"> 
     <input type="password" name="log_pass"> 
     <button type="submit">Login</button> 
    </form> 
    {% if flash.error %} 
     Error: {{ flash.error }} 
    {% endif %} 
</body> 
</html> 

OLD ANSWER

你的問題應該是connected to the PHP session。你必須把它放在你的文件的頂部:

session_start(); 
+0

不,我正在使用Slim的餅乾經理,所以我不需要手動啓動一個PHP會話。使用$ app-> flash似乎不適合我。 – Pepzter9

+0

你好嗎?請使用Slim的Cookie管理員代碼更新您的問題。 –

+0

已更新。查看主要問題 – Pepzter9

0

編輯:

我誤解了問題,我想你正在尋找的是一個簡單的嘗試catch結構定義了你的自定義異常喜歡這個。

class LoginException extends Exception 
{ 
    public function __construct($message, $code = 0, Exception $previous = null) { 
     parent::__construct($message, $code, $previous); 
    } 

    public function __toString() { 
     return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; 
    } 
} 

你的代碼看起來像這樣。

public function login($username, $password){ 
    if(!empty($username)){ 
     if(!empty($password)){ 

      if($this->numrows($username, $password)){ 

       // login success 
       return true; 


      }else{ throw new CustomException('Login error', 1);} 

     }else{throw new CustomException('password error', 2);} 
    }else{throw new CustomException('username error', 3);} 
} 

try { 
if($app->user->login($post->log_mail, $post->log_pass)){ 
     $app->setCookie('mail', $post->log_mail, '2 minutes'); 
     $app->redirect('/inbox'); 
    } 
} catch (CustomException $e) { 
    // Handle the different codes 
} 

在這裏定義了一個異常,你給的消息和代碼,這將代表您的不同錯誤情況。知道你可以在代碼中以不同的方式處理它們,並且當它們發生時,只需將相關的代碼拋出異常即可。


你在找什麼是調用自定義錯誤,如在http://docs.slimframework.com/errors/500/描述。

引發自定義錯誤看起來像這樣。您可以在哪裏調用帶有自定義錯誤的模板等等。在提交部分沒有必要使用if邏輯。

$app->error(function (\Exception $e) use ($app) { 
    DoSomethingElse(); 
    $app->render('mycustomerror.php'); 
}); 
+0

這不顯示應用程序級別的錯誤?我試圖給用戶一個樣式的消息說諸如「不正確的密碼」,我認爲$ app-> flash()的作品var_dump()整個陣列,但我目前有問題顯示一個單一的Flash錯誤。 我在類函數上使用if語句的原因是檢查它是否返回true,如果(返回true){//做某事} else {顯示錯誤} – Pepzter9

+0

這是我目前嘗試過的: $ app-> flash('error','incorrect!'); $ flash = $ app-> view() - > getData('flash'); print $ flash ['error']; (沒什麼(很確定這是由於某種原因reuturning NULL) var_dump($ flash); //這會轉儲所有的數據,我也會在這裏看到錯誤信息。 – Pepzter9

+0

更新了答案 –

相關問題