在多站点应用程序中开发Web页面

时间:2021-10-20 16:27:40

I'm developing several web sites in Kohana using its template system so that one code base is running several web sites using the same back end DB. I'm starting to see a lot of if statements in the views to say if this site do this, if that site do that. It starting to look very non-MVC or Object Oriented. Do other developers have any rules they follow when deciding to breakout view into separate partial views? I want to reuse as much of the code as possible but not swim in if statement in every view. Any reference would be great also.

我正在使用其模板系统在Kohana开发几个网站,以便一个代码库使用相同的后端数据库运行多个网站。我开始在视图中看到很多if语句,如果该站点执行此操作,如果该站点执行此操作。它开始看起来非MVC或面向对象。在决定将视图拆分为单独的部分视图时,其他开发人员是否有任何遵循的规则?我希望尽可能多地重用代码,但不要在每个视图中使用if语句。任何参考也会很棒。

3 个解决方案

#1


0  

If you are using Kohana, you should use modules for stuff you don't want to duplicate for every application. Then you can keep specifics in your application with extended classes or specific settings in config files.

如果您正在使用Kohana,则应该使用模块来处理您不希望为每个应用程序复制的内容。然后,您可以使用扩展类或配置文件中的特定设置保留应用程序中的细节。

http://kohanaframework.org/3.2/guide/kohana/modules

http://kohanaframework.org/3.2/guide/kohana/modules

A lot of if statements are mostly an indication that you need to do refactoring. A large cascading if statement to allow for different sites to be loaded is bad practice in the sense that you make files tightly coupled causing to edit multiple files when you need to make a simple addition or change. Furthermore, it will eventually become ugly if every site needs to load different dependencies or settings or whatever in your if statement.

很多if语句主要表明你需要进行重构。允许加载不同站点的大型级联if语句是一种不好的做法,因为当您需要进行简单的添加或更改时,您会使文件紧密耦合,从而导致编辑多个文件。此外,如果每个站点需要在if语句中加载不同的依赖项或设置或其他内容,它最终会变得很丑陋。

It's difficult to say what you need to change without seeing the code, but try looking at design patterns like the factory or abstract factory design patterns to create site objects.

如果不看代码就很难说你需要改变什么,但是尝试查看工厂或抽象工厂设计模式等设计模式来创建网站对象。

A good book that deals with the subject of patterns and best practices with PHP is PHP 5: Objects, Patterns and Practice by Matt Zandstra: http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804. A very good book.

使用PHP处理模式和最佳实践主题的好书是PHP 5:Matt Zandstra的对象,模式和实践:http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/ 1590593804。一本非常好的书。

#2


0  

I had a similar issue cropping up with this, to keep the views nice and neat I extended the view class into a theme class. I would give the factory method two (or more) views ie Theme::factory(array('site1/home','default/home'),$data)->bind(...); If a themed view existed it would use that, else just serve up the default. That way I, or someone else less competent could easily override to display structure and view behaviour.

我有一个类似的问题,为了保持视图的美观和整洁,我将视图类扩展为主题类。我会给工厂方法两个(或更多)视图,即Theme :: factory(array('site1 / home','default / home'),$ data) - > bind(...);如果存在主题视图,它将使用它,否则只提供默认值。这样,我或其他不太称职的人可以轻易地覆盖显示结构和查看行为。

The class looks like this

这堂课看起来像这样

class Theme extends View {

public static function factory($pages = NULL, array $data = NULL){
  if (is_string($pages)) {
    $pages=array($pages);
  } 
  if (!is_array($pages)) {
    $pages=array(0=>null);
  }


foreach ($pages as $page){
  if ((Kohana::find_file('views', 'themes/'.$page)) !== FALSE){
    return new View('themes/'.$page,$data);
  }
}

throw new Kohana_View_Exception("None of the requested views ':file' could not be found.", array(
            ':file' => join($pages,"' or '"),
  ));
} 
}

#3


0  

You really want ViewModels, only you don't know how to name them yet. Little birds have been tweeting that it'll be in Kohana 3.3 but unless you have a year or so to wait, I recommend trying either View-Model module by Zombor or moving away from Kohana's templating system whatsoever in favour of KOstache (it's Mustache for Kohana)

你真的想要ViewModels,只是你不知道如何命名它们。小鸟一直在发推文说它会在Kohana 3.3中,但是除非你有一年左右的时间等待,我建议你尝试使用Zombor的View-Model模块,或者远离Kohana的模板系统,无论如何都支持KOstache(它是Mustache for Kohana的)

If for any reason you want to stick with your current codebase, I'd go with splitting the view into several smaller interchangable parts and loading them when necessary (echo View::Factory(($something == 'one' ? 'view_one' : 'view_two')) or a custom written helper would be a nice toy here)

如果出于任何原因你想要坚持使用当前的代码库,我会将视图分成几个较小的可互换部分并在必要时加载它们(echo View :: Factory(($ something =='one'?'view_one' :'view_two'))或者自定义书面助手在这里会是一个不错的玩具)

#1


0  

If you are using Kohana, you should use modules for stuff you don't want to duplicate for every application. Then you can keep specifics in your application with extended classes or specific settings in config files.

如果您正在使用Kohana,则应该使用模块来处理您不希望为每个应用程序复制的内容。然后,您可以使用扩展类或配置文件中的特定设置保留应用程序中的细节。

http://kohanaframework.org/3.2/guide/kohana/modules

http://kohanaframework.org/3.2/guide/kohana/modules

A lot of if statements are mostly an indication that you need to do refactoring. A large cascading if statement to allow for different sites to be loaded is bad practice in the sense that you make files tightly coupled causing to edit multiple files when you need to make a simple addition or change. Furthermore, it will eventually become ugly if every site needs to load different dependencies or settings or whatever in your if statement.

很多if语句主要表明你需要进行重构。允许加载不同站点的大型级联if语句是一种不好的做法,因为当您需要进行简单的添加或更改时,您会使文件紧密耦合,从而导致编辑多个文件。此外,如果每个站点需要在if语句中加载不同的依赖项或设置或其他内容,它最终会变得很丑陋。

It's difficult to say what you need to change without seeing the code, but try looking at design patterns like the factory or abstract factory design patterns to create site objects.

如果不看代码就很难说你需要改变什么,但是尝试查看工厂或抽象工厂设计模式等设计模式来创建网站对象。

A good book that deals with the subject of patterns and best practices with PHP is PHP 5: Objects, Patterns and Practice by Matt Zandstra: http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804. A very good book.

使用PHP处理模式和最佳实践主题的好书是PHP 5:Matt Zandstra的对象,模式和实践:http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/ 1590593804。一本非常好的书。

#2


0  

I had a similar issue cropping up with this, to keep the views nice and neat I extended the view class into a theme class. I would give the factory method two (or more) views ie Theme::factory(array('site1/home','default/home'),$data)->bind(...); If a themed view existed it would use that, else just serve up the default. That way I, or someone else less competent could easily override to display structure and view behaviour.

我有一个类似的问题,为了保持视图的美观和整洁,我将视图类扩展为主题类。我会给工厂方法两个(或更多)视图,即Theme :: factory(array('site1 / home','default / home'),$ data) - > bind(...);如果存在主题视图,它将使用它,否则只提供默认值。这样,我或其他不太称职的人可以轻易地覆盖显示结构和查看行为。

The class looks like this

这堂课看起来像这样

class Theme extends View {

public static function factory($pages = NULL, array $data = NULL){
  if (is_string($pages)) {
    $pages=array($pages);
  } 
  if (!is_array($pages)) {
    $pages=array(0=>null);
  }


foreach ($pages as $page){
  if ((Kohana::find_file('views', 'themes/'.$page)) !== FALSE){
    return new View('themes/'.$page,$data);
  }
}

throw new Kohana_View_Exception("None of the requested views ':file' could not be found.", array(
            ':file' => join($pages,"' or '"),
  ));
} 
}

#3


0  

You really want ViewModels, only you don't know how to name them yet. Little birds have been tweeting that it'll be in Kohana 3.3 but unless you have a year or so to wait, I recommend trying either View-Model module by Zombor or moving away from Kohana's templating system whatsoever in favour of KOstache (it's Mustache for Kohana)

你真的想要ViewModels,只是你不知道如何命名它们。小鸟一直在发推文说它会在Kohana 3.3中,但是除非你有一年左右的时间等待,我建议你尝试使用Zombor的View-Model模块,或者远离Kohana的模板系统,无论如何都支持KOstache(它是Mustache for Kohana的)

If for any reason you want to stick with your current codebase, I'd go with splitting the view into several smaller interchangable parts and loading them when necessary (echo View::Factory(($something == 'one' ? 'view_one' : 'view_two')) or a custom written helper would be a nice toy here)

如果出于任何原因你想要坚持使用当前的代码库,我会将视图分成几个较小的可互换部分并在必要时加载它们(echo View :: Factory(($ something =='one'?'view_one' :'view_two'))或者自定义书面助手在这里会是一个不错的玩具)