简体   繁体   English

CakePHP-文件结构混乱-不同的控制器,或全部位于同一控制器中?

[英]CakePHP - file structure confusion - different controllers, or all in same?

To understand my confusion/question, let me first give a quick overview of the site I'm building: The site has a homepage, and 7 category pages, each with the same/similar layout. 为了理解我的困惑/问题,首先让我快速概述一下我正在构建的网站:该网站有一个主页和7个类别页面,每个页面具有相同/相似的布局。

On most category homepages (eg "Dining"), you can do a search (ie a search for restaurants), which will take you to a results page. 在大多数类别的首页(例如“餐厅”)上,您可以进行搜索(即搜索餐馆),这将带您进入结果页面。 This will have the same/similar layout as the... "Theaters" search result page...etc etc. 这将具有与...“剧院”搜索结果页面...等相同/相似的布局。

So - although there are a lot of sections, there are not a lot of page layouts. 所以-尽管有很多部分,但页面布局却不多。

Question: I'm not sure if I should create separate controllers for each, and just use the same CSS for them? 问:我不确定是否应该为每个控制器创建单独的控制器,并为它们使用相同的CSS? Or if I can use the same view file, but populate each content area on the page differently based on the url/action..etc? 还是我可以使用相同的视图文件,但是根据url / action..etc不同地填充页面上的每个内容区域?

They'll all reference different & multiple tables in the database - for instance, restaurants will reference the restaurants table, the cuisines table, the zip_codes table...etc. 他们都将引用数据库中的不同表和多个表-例如,餐厅将引用餐厅表,美食表,邮政编码表等。 Theaters might reference the "movies" table, the zip_codes table...etc etc etc 剧院可能会引用“电影”表,邮政编码表...等等等

But - I also want an admin tool for some - ie "restaurants/edit" or "theaters/edit" ...etc - not ALL the pages will have admin for that specific url - ie "outdoors" might just be "businesses/edit" or something generic... I don't know - I'm confused as you can tell. 但是-我也想要一些管理工具-例如“餐厅/编辑”或“剧院/编辑” ...等等-并非所有页面都具有该特定网址的管理-即“户外”可能只是“企业/编辑”或其他通用名称...我不知道-如您所知,我很困惑。

Any help is GREATLY appreciated. 任何帮助是极大的赞赏。 I've read a lot about the technical things to do w/ CakePHP, but I can't find any good resources for general project structures for varying scenarios. 我已经阅读了很多关于使用CakePHP要做的技术性事情,但是我找不到适用于各种情况的常规项目结构的任何好资源。

You can answer this yourself. 您可以自己回答。 Suppose you implement separate controllers, models and views for each, how do they differ? 假设您为每个实现单独的控制器,模型和视图,它们有何不同? If they only differ in the table names and labels/strings that they use, then you can use just one generic controller. 如果它们仅在使用的表名和标签/字符串上有所不同,则您只能使用一个通用控制器。 But, if they are different in other ways (eg the Restaurants model has a totally different relationship structure than a Movie model) then use separate controllers. 但是,如果它们在其他方面有所不同(例如,餐厅模型与电影模型具有完全不同的关系结构),则可以使用单独的控制器。

To prevent too many repetition between controllers and view files when you do use separate controllers, try to push functionality in a common base controller (eg AppController, of create your own base controller based on AppController that your MoviesController and RestaurantsController and so on will derive from). 为了避免在使用单独的控制器时控制器和视图文件之间出现过多重复,请尝试在通用基本控制器(例如AppController)中推送功能,该功能是基于MovieController和RestaurantsController等将基于AppController创建自己的基本控制器的)。 Push generic view code into elements. 将通用视图代码放入元素中。

A word about CSS: Of course you can (and should) reuse that. 关于CSS的一句话:当然,您可以(并且应该)重用它。 It's mostyly separate from the view. 它通常与视图分开。 Just use sensible class names and id's in your views and the CSS will be easy to reuse. 只需在您的视图中使用明智的类名和ID,CSS就将易于重用。

If you are still unsure, I'd go with separate controllers. 如果您仍然不确定,我将使用单独的控制器。 It may be more work but it will be cleaner and easier to debug. 可能需要做更多的工作,但是会更清洁,更容易调试。 Use the above suggestions to avoid repetition. 使用以上建议避免重复。

-group elements that will share among all site in a layout (ie. site css, site common js library) -make a view for each of your category(page) (ie. dining page, theater page, restaurants page) and put there page only stuff (ie. theater js, theater includes) there - in theory, every vew should have a controller and model, so you have to make them separate. -将在布局中的所有站点之间共享的组元素(例如,站点css,站点通用js库)-为每个类别(页面)(例如,餐饮页面,剧院页面,餐厅页面)创建一个视图,并将其放置在此处仅在页面上显示内容(例如,剧院js,剧院包括)-理论上,每个vew都应具有控制器和模型,因此您必须将它们分开。

anyway url like "restaurants/edit" or "theaters/edit" actually means model "restaurant", operation "edit" in your controller file. 无论如何,像“餐厅/编辑”或“剧院/编辑”之类的网址实际上意味着控制器文件中的模型“餐厅”,操作“编辑”。

I suggest you read the blog tutorial from cakephp site, you that should answer all your confusion 我建议您阅读cakephp网站上的博客教程 ,您应该回答所有的困惑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM