CakePHP,围绕组组织站点结构

发布于 2024-09-15 05:22:47 字数 1297 浏览 15 评论 0原文

因此,我不太确定应该如何在 CakePHP 中构建它,以便以正确的 MVC 形式正常工作。

为了便于论证,假设我有以下以各种方式相关的数据结构:

  • 团队
  • 任务
  • 设备

这通常是站点的样子,并且在 Cake 中很容易构建和制作。例如,我将为每个项目集提供模型、控制器和视图。

我的问题(我确信无数其他人已经遇到过并且已经解决了)是我的级别高于项目集。因此,例如:

  • 部门
    • 团队
    • 任务
    • 设备
    • 团队
    • 任务
    • 设备
    • 团队
    • 任务
    • 设备

在我的站点中,我需要某人能够在单个组级别查看该站点,并能够一起查看所有站点(即忽略组)。

因此,我有出发、团队、任务和设备的模型、视图和控件。

如何构建我的网站,以便某人可以从部门视图中选择一个部门,然后在网站中移动到团队/任务/设备的不同视图,仅显示属于该特定部门的视图。

在相同的格式中,是否有一种方法可以忽略部门关联?

希望以下示例 URL 能够澄清任何不清楚的内容:

// View items while disregarding which group-set record they belong to
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id

http://www.example.com/Departments

// View items as if only those associated with the selected group-set record exist
http://www.example.com/Department/HR/Team/action/id
http://www.example.com/Department/HR/Task/action/id
http://www.example.com/Department/HR/Equipment/action/id

我可以让控制器以这种方式运行吗?有没有人可以阅读以便我能弄清楚这一点?

感谢那些阅读了所有这些内容的人:)

So, I'm not quite sure how I should structure this in CakePHP to work correctly in the proper MVC form.

Let's, for argument sake, say I have the following data structure which are related in various ways:

  • Team
  • Task
  • Equipment

This is generally how sites are and is quite easy to structure and make in Cake. For example, I would have the a model, controller and view for each item set.

My problem (and I'm sure countless others have had it and already solved it) is that I have a level above the item sets. So, for example:

  • Department
    • Team
    • Task
    • Equipment
  • Department
    • Team
    • Task
    • Equipment
  • Department
    • Team
    • Task
    • Equipment

In my site, I need the ability for someone to view the site at an individual group level as well as move to view it all together (ie, ignore the groups).

So, I have models, views and controls for Depart, Team, Task and Equipment.

How do I structure my site so that from the Department view, someone can select a Department then move around the site to the different views for Team/Task/Equipment showing only those that belong to that particular Department.

In this same format, is there a way to also move around ignoring the department associations?

Hopefully the following example URLs clarifies anything that was unclear:

// View items while disregarding which group-set record they belong to
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id

http://www.example.com/Departments

// View items as if only those associated with the selected group-set record exist
http://www.example.com/Department/HR/Team/action/id
http://www.example.com/Department/HR/Task/action/id
http://www.example.com/Department/HR/Equipment/action/id

Can I get the controllers to function in this manner? Is there someone to read so I can figure this out?

Thanks to those that read all this :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰沐子 2024-09-22 05:22:47

我想我知道你想做什么。如果我错了,请纠正我:

我为自己构建了一个项目管理器,我希望其中的 URL 更符合逻辑,因此不要使用

http://domain.com/project/milestones/add/MyProjectName 我可以使用
http://domain.com/project/MyProjectName/milestones/add

我添加了自定义路线到我的路线的末尾(!重要),以便它捕获任何还不是路线的内容并将其视为“可变路线”。

Router::connect('/project/:project/:controller/:action/*', array(), array('project' => '[a-zA-Z0-9\-]+'));

无论您采用哪种路线,都意味着您不能已经(或永远)拥有使用该名称的控制器,因此我认为使用单数单词而不是复数是一个很好的做法。 (我有一个项目控制器,因此我使用“project”来避免与其冲突。)

现在,要在应用程序中的任何位置访问 :project 参数,我在 AppController 中使用此函数:

function __currentProject(){
// Finding the current Project's Info
    if(isset($this->params['project'])){
        App::import('Model', 'Project');
        $projectNames = new Project;
        $projectNames->contain();
        $projectInfo = $projectNames->find('first', array('conditions' => array('Project.slug' => $this->params['project'])));
        $project_id = $projectInfo['Project']['id'];
        $this->set('project_name_for_layout', $projectInfo['Project']['name']);
        return $project_id;
    }
}

并且在其他控制器中使用它:

function overview(){
    $this->layout = 'project';

    // Getting currentProject id from App Controller
    $project_id = parent::__currentProject();

    // Finding out what time it is and performing queries based on time.
    $nowStamp = time();

    $nowDate = date('Y-m-d H:i:s' , $nowStamp);
    $twoWeeksFromNow = $nowDate + 1209600;

    $lateMilestones = $this->Project->Milestone->find('all', array('conditions'=>array('Milestone.project_id' => $project_id, 'Milestone.complete'=> 0, 'Milestone.duedate <'=> $nowDate)));
    $this->set(compact('lateMilestones'));

    $currentProject = $this->Project->find('all', array('conditions'=>array('Project.slug' => $this->params['project'])));
    $this->set(compact('currentProject'));
}

对于您的项目,您可以尝试在 paths.php 文件末尾使用这样的路由:

Router::connect('/:groupname/:controller/:action/*', array(), array('groupname' => '[a-zA-Z0-9\-]+'));

// 注意,我从开头删除了“/project”。如果您将 :groupname 放在第一位,就像我在上一个示例中所做的那样,那么这些自定义 url 路由只有一个选项。

然后根据您的需要修改其他代码。

I think I know what you're trying to do. Correct me if I'm wrong:

I built a project manager for myself in which I wanted the URLs to be more logical, so instead of using something like

http://domain.com/project/milestones/add/MyProjectName I could use
http://domain.com/project/MyProjectName/milestones/add

I added a custom route to the end (!important) of my routes so that it catches anything that's not already a route and treats it as a "variable route".

Router::connect('/project/:project/:controller/:action/*', array(), array('project' => '[a-zA-Z0-9\-]+'));

Whatever route you put means that you can't already (or ever) have a controller by that name, for that reason I consider it a good practice to use a singular word instead of a plural. (I have a Projects Controller, so I use "project" to avoid conflicting with it.)

Now, to access the :project parameter anywhere in my app, I use this function in my AppController:

function __currentProject(){
// Finding the current Project's Info
    if(isset($this->params['project'])){
        App::import('Model', 'Project');
        $projectNames = new Project;
        $projectNames->contain();
        $projectInfo = $projectNames->find('first', array('conditions' => array('Project.slug' => $this->params['project'])));
        $project_id = $projectInfo['Project']['id'];
        $this->set('project_name_for_layout', $projectInfo['Project']['name']);
        return $project_id;
    }
}

And I utilize it in my other controllers:

function overview(){
    $this->layout = 'project';

    // Getting currentProject id from App Controller
    $project_id = parent::__currentProject();

    // Finding out what time it is and performing queries based on time.
    $nowStamp = time();

    $nowDate = date('Y-m-d H:i:s' , $nowStamp);
    $twoWeeksFromNow = $nowDate + 1209600;

    $lateMilestones = $this->Project->Milestone->find('all', array('conditions'=>array('Milestone.project_id' => $project_id, 'Milestone.complete'=> 0, 'Milestone.duedate <'=> $nowDate)));
    $this->set(compact('lateMilestones'));

    $currentProject = $this->Project->find('all', array('conditions'=>array('Project.slug' => $this->params['project'])));
    $this->set(compact('currentProject'));
}

For your project you can try using a route like this at the end of your routes.php file:

Router::connect('/:groupname/:controller/:action/*', array(), array('groupname' => '[a-zA-Z0-9\-]+'));

// Notice I removed "/project" from the beginning. If you put the :groupname first, as I've done in the last example, then you only have one option for these custom url routes.

Then modify the other code to your needs.

┊风居住的梦幻卍 2024-09-22 05:22:47

如果这是一个公共站点,您可能需要考虑使用命名变量。这将允许您仍然在 URL 上定义组,但没有额外的功能要求。

http://example.com/team/group:hr
http://example.com/team/action/group:hr/other: var

它可能也需要自定义路由...但它应该可以完成这项工作。

http://book.cakephp.org/view/541/Named-parameters
http://book.cakephp.org/view/542/Defining-Routes

If this is a public site, you may want to consider using named variables. This will allow you to define the group on the URL still, but without additional functionality requirements.

http://example.com/team/group:hr
http://example.com/team/action/group:hr/other:var

It may require custom routes too... but it should do the job.

http://book.cakephp.org/view/541/Named-parameters
http://book.cakephp.org/view/542/Defining-Routes

梦冥 2024-09-22 05:22:47

会话

由于网络是无状态的,因此您需要使用会话(或 cookie)。您需要问自己的问题是如何反映特定部门的选择(或不选择)。它可以很简单,只需在右上角放置一个反映全部、人力资源、销售等的下拉选项即可。当下拉列表更改时,它将设置(或清除)组会话变量。

至于控制器中的功能,您只需检查会话即可。如果存在,您可以按选择的组限制数据。因此,您将使用相同的 URL,但控制器或模型将管理数据的显示方式。

// for all functionality use:
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id

您无需更改 URL 来适应该功能。这就像为每个想要查看其地址、电话号码或帐单信息的用户使用不同的 URL。其中 USER 是组,地址、电话号码< BILLING INFO 是项目集。

没有会话

另一种选择是将组过滤器放在每个页面上。例如,在团队/索引视图中,您将有一个组下拉菜单来过滤数据。它可以完成同样的事情,而无需设置和清除会话变量。

结论是,要记住的关键是功能不会改变,URL 也不会改变。唯一改变的是您将使用过滤后的数据集。

这有道理吗?

SESSIONS

Since web is stateless, you will need to use sessions (or cookies). The question you will need to ask yourself is how to reflect the selection (or not) of a specific department. It could be as simple as putting a drop down selection in the upper right that reflects ALL, HR, Sales, etc. When the drop down changes, it will set (or clear) the Group session variable.

As for the functionality in the controllers, you just check for the Session. If it is there, you limit the data by the select group. So you would use the same URLs, but the controller or model would manage how the data gets displayed.

// for all functionality use:
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id

You don't change the URL to accommodate for the functionality. That would be like using a different URL for every USER wanting to see their ADDRESS, PHONE NUMBER, or BILLING INFO. Where USER would be the group and ADDRESS, PHONE NUMBER< and BILLING INFO would be the item sets.

WITHOUT SESSIONS

The other option would be to put the Group filter on each page. So for example on Team/index view you would have a group drop down to filter the data. It would accomplish the same thing without having to set and clear session variables.

The conclusion is and the key thing to remember is that the functionality does not change nor does the URLs. The only thing that changes is that you will be working with filtered data sets.

Does that make sense?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文