在控制器mvc中处理switch语句

时间:2013-01-09 19:35:21

标签: php model-view-controller design-patterns switch-statement

我的控制器类中有很多开关盒。我可以减少开关盒或更好地处理控制器中的开关盒。 我不得不使用开关盒处理按钮和链接。 我可以使用工厂模式吗? 请看我的代码结构:

class manageController
{
private $controller;
private $action;

public function __construct($controller,$action)
{
    $this->controller=$controller;
    $this->action=$action;
}
public function dispatch()
{  ...  }
private function indexAction()//for index.php
{  ...  }
private function managecontentAction()//for managecontent.php
{
    ...
           if(isset($_POST['delete_page'])){
             ...        
    }elseif(isset($_POST['save_page'])){
    ...
    }elseif(isset($_POST['reset_subject_menu'])){
    ...
    }elseif(isset($_POST['remove_subject_menu'])){  
    ...
    }elseif(isset($_POST['insert_subject_menu'])){
    ...
    }elseif(isset($_POST['update_subject_menu'])){
    ...
    }elseif(isset($_GET['subject_menu_id'])){
    ... 
    }elseif(isset($_GET['action']) && $_GET['action']=='add-subject-menu'){
            ...     
    }elseif(isset($_GET['action']) && $_GET['action']=='add-page-menu'){
    ... 
    }
    ...
    $this->render();
}
 }

 ?>

我在处理url时遇到了一些问题。例如,当我输入http://localhost/manage/managecontent时,它加载良好,但是当我输入http://localhost/manage/managecontent/ html加载但css文件和图像不加载时!
请参阅bootstrap中的代码结构:

<?php
if(!defined('ROOT_PATH')){
define('ROOT_PATH','../');
}

require_once ROOT_PATH.'function/my_autoloader.php';

use application\controllers as controllers;

$uri=strtolower($_SERVER['REQUEST_URI']);
$actionName='';
$uriData=array();
$uriData=preg_split('/[\/\\\]/',$uri );

$actionName = (!empty($uriData[3])) ? preg_split('/[?].*/', $uriData[3] ): '' ;
$actionName =$actionName[0];

if(empty($controllerName))
die('ERROR your request page doesn\'t exist!');
else {
if(empty($actionName))
    $actionName='index';
}
switch ($controllerName) {
case 'manage':
    $controller = new Controllers\manageController($controllerName,$actionName);
    break;
default:
    die('ERROR WE DON\'T HAVE THIS ACTION!');
    exit;
    break;
 }
 // function dispatch send url to controller layer
 $controller->dispatch();
?>

我的.htaccess文件是:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ public/index.php [NC,L] 

0 个答案:

没有答案