2015-09-05 126 views
-1

目前我用switch($_GET['page'])来简单说明。这是一个简单的解决方案,基本上在任什么是处理页面切换的最佳方式?

然而,可悲的是,一些项目增长如此之多,我想知道,如果有更好的更快的方法呢?

这是我的基地,我怎么切换页面目前:

// There is more complex .htacces to translation of friendly-urls behind it, but for example sake, these variables are being produced: 
$lext = array(
    'parent_page' => 'about-us', 
    'child_page' => 'map-to-somewhere', 
    'child_id' => NULL, // if it would be a article or something, it would be example.com/{parent_page}/{child_id}-some-friendly-url.html 
); 

switch ($lext['parent_page']) { 
    case 'about-us': 
     // about us page 
    break; 
    case '': 
     // home 
    break; 
    default: 
     // 404 
    break; 
} 

开关内部的情况下,它要么会触发一个函数或包含文件。事实证明,产生最快的页面加载结果。

所以我想知道,对于大量的流量和你的“index.php”aka。登陆文件得到很多命中。什么是最快和最简单的解决方案?
作为最简单的或愚蠢的解决方案似乎产生最好的结果,我woulnt感到惊讶,如果:

if ($lext['parent_page'] == 'about-us') { 
    // about us page 
} else if ($lext['parent_page'] == '') { 
    // home 
} else { 
    // 404 
} 

..would更快更好beforming然后switch()

我已经搜索过类似的问题,并测试了所有的答案,但是我发现的答案不会更好。

+0

我们在这里讨论了多少页?因为如果你只有3页,我怀疑如果你使用不同的方法,性能会增加。 – Rimble

+0

@TomKriek该网站获得大量的点击量,少于10页。但可能有更多的子页面。但是,他们正在从功能或包含内部处理。所以目前,这个页面切换部分正在减缓页面加载的最多。其他项目,流量小,没有任何问题。但是这个网站目前大约有600个用户,并且它变得有问题。 –

+0

复制重复...搜索的副本。 –

回答

1

对此的多个答案。很大程度上取决于您的项目以及您想要做多少重构。我的担心不会像代码可扩展性和易维护性那么快。一个switch除了很多案件,可能不会导致任何明显的减速与if-else或其他方式。

一种方法可能是进入MVC框架的世界,MVC框架通常每个页面都有一个控制器方法,允许您在代码中进行清晰的分割。例如,代码点火器,你正是如此指定网页:

class MySite { 

    /* constructor etc */ 

    public function page_1() { 
     //this is called on visits to /page_1 
     //load a view for this page, etc 
    } 

    public function page_13() { 
     //this is called on visits to /page_3 
     //load a view for this page, etc 
    } 

} 

更简单的方法可能是使现有的情况下,JSON数据文件,并在每一个要发生什么。

{ 
    "page_1": {"inc": "page_1.php"}, 
    "page_13": {"func:": "some_func"}, 
} 

然后,在你的PHP:

//get data 
$data = @file_get_contents($fp = 'pages_data.txt') or die("Couldn't load ".$fp); 
$data = json_decode($data, 1); 

//look for requested page in data - if found, include file or run function... 
if (isset($data[$lext['parent_page']])) { 

    $item = $data[$lext['parent_page']]; 

    //...include file 
    if (isset($item['inc']) && file_exists($item['inc'])) 
     include $item['inc']; 

    //...run function 
    else if (isset($item'func']) && function_exists($item['func'])) 
     $item['func'](); 

} else 
    //404... 
} 
+1

我看,非常有趣的做法。我会试试看。作为JSON,我们现在使用的设置。从SQL中获取它们或对其进行memcaching,速度较慢。 –

0

取决于你如何管理你的网页。如果你有require每一页的文件,不是你总是可以通过简单地加载该文件:

$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 'index'; 

if (file_exists(__DIR__.'/views/'.$page.'.php')) { 
    require(__DIR__.'/views/'.$page.'.php'); 
} else { 
    switch ($page) { 
     // Custom rules that does not fit to previous rule. 
    } 
} 

我建议使用类/动作结构动态加载请求的页面(最喜欢的框架)。

[index.php] 

$route = isset($_REQUEST['route']) ? $_REQUEST['route'] : 'index'; 
$page = explode('/', $route); 

require_once(__DIR__.'/controller/'.ucfirst($route[0]).'Controller.php'); 

$className = ucfirst($route[0]).'Controller'; 
$class = new $className(); 
$class->{$route[1]}(); 

一些警告

应尽量将白名单您的请求,请不要忘记为空值,默认值,使用$_REQUEST如果你可以通过POST传递路由信息或GET。


对于SEO网址,您将使用.htaccess和数据库。

+0

note:清理'$ page'(例如:'../') –

相关问题