2012-03-11 18 views
0

MVC - 如何使URL不會改變時,一個方法被調用

登錄控制器,模型和視圖

網址http://mySite/login

現在,當我在http://mySite/controller它顯示的登錄形式,然後當我提交表格運行方法被稱爲所以url更改爲http://mySite/login/run

我怎麼能阻止這個:?

PS //我創造我自己的MVC下面這個嘖嘖:http://www.youtube.com/watch?v=2Eu0Nkpo6vM

登錄控制器

class Login extends Conroller { 

    function __construct() { 
     parent::__construct(); 
    } 

    function index() 
    { 
     $this->view->render('authentication/enter'); 
    } 

    function run() 
    { 
     $this->model->run(); 
    } 


} 

登錄模型

class Login_Model extends Model 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function run() 
    { 

      $sth = $this->dbh->connect()->prepare("SELECT UserID FROM users WHERE 
       username = :login "); 
     $sth->execute(array(':login' => $_POST['login'])); 

     $data = $sth->fetch(); 

     $count = $sth->rowCount(); 
     if ($count > 0) { 
      // login 
      Session::set('loggedIn', $_POST['login']); 
      header('location: ../dashboard'); 
     } else { 
      echo 4; 
     } 


    } 

} 

全無.htacces

的網址是

http://mySite/index.php?url= {控制器名}

http://mySite/index.php?url= {控制器名稱}/{從位指示一些方法}

+0

請顯示您的一些代碼,以便我們可以找出重定向的位置。 – 2012-03-11 04:18:12

+0

我編輯帖子 – Ben 2012-03-11 04:24:14

+0

在tut中,當你在/index.php?url=login加載登錄控制器時加載登錄模型和視圖。 Everething的工作是如何顯示在教程中,但劑量劑顯示如何更改網址 – Ben 2012-03-11 04:30:20

回答

0

如果沒有看整部影片還是想知道太辛苦,爲什麼你重新發明PHP MVC輪子,這是我最好的猜測。

  1. 更改您的視圖的形式從那裏

    function index() 
    { 
        if ($this->request->isPost()) { // seriously, I'm just guessing here 
         $this->model->run(); 
        } 
        $this->view->render('authentication/enter'); 
    } 
    
提交索引操作

<form method="post" action="login" ... 
  • 索引中的作用,檢測POST請求,並運行模型的run()方法

    幾個筆記...

    • 不要從模型
    • 始終exit;做控制器任務(即重定向)發送Location響應頭
    • 刪除這個爛攤子後,並嘗試像Symfony的或Zend的
    經過驗證和測試的MVC框架
  • +0

    OOP是

    Ben 2012-03-11 04:36:58

    +0

    @MarianPetrov我現在喜歡Symfony 2,但這只是我的看法 – Phil 2012-03-11 04:42:38

    相關問題