0

我在我的所有视图中包含的 header.php 文件中有以下内容:

$dh = opendir(Vs.get_class($this).'/js') ;
while($script = readdir($dh)) {
    if(!is_dir($script))
    {
        echo '<script type="text/javascript" src="js/'.$script.'"></script>' ;
    }
}

$dh = opendir(Vs.get_class($this).'/css') ;
while($css = readdir($dh)) {
    if(!is_dir($css))
    {
        echo '<link type="text/css" href="css/'.$css.'" rel="stylesheet"/>' ;
    }
}

它的目的是自动加载特定视图的所有 css 和 JS 文件(与控制器同名,因此get_class)。

所有这些都应该是关联控制器的一部分,还是我做得很好?

4

2 回答 2

2

Dude just use Glob php function . And then include those.. it will work perfectly fine. Also you will need to pass the absolute path

foreach (glob("*.css") as $filename) 
{ 
   echo '<link type="text/css" href="css/'.$filename.'" rel="stylesheet"/>' ; 
}
于 2012-12-04T11:40:31.350 回答
1

Scripts and stuff which are only and exclusively used in the View belong in the View. The controller has nothing to do with it.

Views don't need to be dumb. In fact, they shouldn't be. They need to care about everything that has to do with preparing and returning a response. That even includes setting HTTP headers and possible caching logic. Including necessary CSS and JS scripts is also part of its job.

于 2012-12-04T11:41:21.327 回答