現在オブジェクト指向(mvcモデル)でリアルタイムチャットを開発中なのですが、どこにjavascriptを組み込めばいいのかわかりません。
/chatin/loginだった場合下のコードのChatinControllerクラスのloginActionメソッドを実行されるように
URLでルーティング
loginAction()内の
return $this->render($variables = array('_token' => $this->generateToken('aoiin/login') ,);
でビューファイル(HTML)と必要な値を出力
$connection = $this->property['db_info']->getDbClass('Member');
でmodelクラスに接続し処理結果を取得。
return $this->property['response']->setHeader('Location' , 'http:');
で chat/welcome にリダイレクト。
ChatControllerクラスのwelcomeActionメソッドを実行し
チャット画面を表示
のようにしてmvcモデルでチャットを開発しているのですが、ここにajaxを組み込めばいいのかわかりません。
ajaxで接続するPHPファイルは手続き型で書いて適当な場所においてmvcモデルとは別にするべきでしょうか。
自分なりに作成したフレームワーク(?)で開発している為わかりづらくてすみませんがお願いします。
PHP
1class ChatinController extends Controller 2{ 3 public function loginAction() 4 { 5 try { 6 if ($this->property['validate']->issetVal(array('name',) , 'post') !== false) {//nameが入力されている時 7 $name = $this->property['request']->getPost(array('name')); 8 $token = $this->property['request']->getPost(array('_token',)); 9 $connection = $this->property['db_info']->getDbClass('Member'); 10 $count = $connection->countUserName($name['name']); 11 if($count == 0 && $connection->countUser() <= 13 12 &&$this->checkToken('aoiin/login' , $token['_token']) !== false){//ユーザー名の重複、入室ユーザ13人以下 13 $connection->insertUserName($name['name']); 14 $this->property['session']->setSession(array('name' => $name['name'])); 15 $this->property['session']->setSession(array('time' => time())); 16 $user_name = $this->property['session']->getSession('name'); 17 18 return $this->property['response']->setHeader('Location' , 'http:'); 19 }elseif($count !== 0) {//ユーザ名重複 20 throw new Exception('This username has been used .'); 21 }elseif ($connection->countUser() > 13) {//満室 22 throw new Exception('Full house'); 23 } 24 }elseif($this->property['validate']->issetVal(array('name',) , 'session') !== false){ 25 return $this->property['response']->setHeader('Location' , 'http:'); 26 } 27 }catch (Exception $e) { 28 return $this->render($variables = array('_token' => $this->generateToken('aoiin/login') , '_error' => $e->getMessage() ,) , $layout = 'aoilayout'); 29 } 30 return $this->render($variables = array('_token' => $this->generateToken('aoiin/login') ,) , $layout = 'aoilayout'); 31 } 32 33 public function logoutAction() 34 { 35 $connection_out = $this->property['db_info']->getDbClass('Out'); 36 $connection_mem = $this->property['db_info']->getDbClass('Member'); 37 if ($connection_out->getLastOutId() >= 6) { 38 $connection_out->deleteOutUser(); 39 } 40 $name = $this->property['session']->getSession('name'); 41 $connection_out->insertOutUser($name); 42 $connection_mem->deleteUser($name); 43 $this->property['session']->removeSession(array('name',)); 44 return $this->property['response']->setHeader('Location' , 'http:'); 45 } 46} 47
PHP
1class ChatController extends Controller 2{ 3 public function welcomeAction() 4 { 5 if ($this->property['validate']->issetVal(array('name',) , 'session')) { 6 $user_name = $this->property['session']->getSession('name'); 7 $this->property['session']->setSession(array('time' => time())); 8 9 return $this->render($variables = array('user' => $user_name , ) , $layout = 'aoilayout'); 10 }elseif ($this->property['session']->getSession('time')+3600 > time() 11 || $this->property['validate']->issetVal(array('name',) , 'session') !==false) { 12 13 return $this->property['response']->setHeader('Location' , 'http://'); 14 } 15 } 16}
回答1件
あなたの回答
tips
プレビュー