PHP OOP核心框架 [英] PHP OOP core framework

查看:169
本文介绍了PHP OOP核心框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是张贴这个问题,所以一些你可能能够指出我的方式正确。我慢慢地加热到OOP,开始理解这个概念。我想要做一个良好的坚实的核心或基础用作CMS后端。它也将使用MVC。我一直在使用 http://gilbitron.github.com/PIP/ 作为MVC基础。

I am just posting this question so some of you might be able to point me in the right way. I am slowly warming up to OOP, starting to understand the concept. I want to make a good solid core or foundation to be used as a CMS backend. It will also use MVC. I have been using http://gilbitron.github.com/PIP/ as the MVC- base.

我无法找到的东西如下:

The thing I cant figure out is the following:

说,在后端的项目页面上我有两个部分:htmltext项目和我应该能够编辑他们。 uri是这样的:
// domain / backend / projects(方法将是索引并显示两个部分)

Say, on the projectpage in the backend I have 2 sections: htmltext and projects and I should be able to edit them both. The uri would be something like: //domain/backend/projects (the method would be the index and show the 2 sections)

当我点击项目应如何处理?
// domain / backend / projects / projects /或
// domain / backend / projects / list /

When i click on projects how should it be handled? //domain/backend/projects/projects/ or //domain/backend/projects/list/

将保存一些图片或画廊:
// domain / backend / projects / edit / 5 / gallery / 2

One step further, a project will hold some images or a gallery: //domain/backend/projects/edit/5/gallery/2

我的问题是,这是一个很好的方法,更重要的是如何在OOP中实现

My question here is, first: would this be a good way to go and even more important how would this be implemented in OOP

主项目控制器:

class projects {

    function index(){
        // view index
    }

    function edit{
        $project = new Project();
        $projectdata = $project->load(5);
    }
}

单个项目控制器

class project {

    function __construct(){
        $this->projectmodel = $this->loadModel('project_model'); // prepare the model to be used
    }

    function load($id){
        $this->projectmodel->loadproject($id);
    }
}

项目模型

class project_model extends model { //extends for DB  access and such

    function __construct(){
        // do stuff
    }

    function loadproject($id){
        return $this->db->query("SELECT * FROM projects where id=" . $id . " LIMIT 1");
    }
}

如果这个项目有图像,我应该在哪里加载图像类来处理这些?我应该加载它在project_model像$ this-> images = new Images();并在模型中有一个函数

Now my question. If this project has images, where should I load the image class to handle those? Should I load it in the project_model like $this->images = new Images(); and have a function inside the model

function loadimages($id){
    $this->images->load($id);
}

,然后图片类似:

class image extends model { //extends for DB  access and such 

    function __construct(){
    }

    function load($id){
        return $this->db->query("SELECT * FROM project_images where id=" . $id . " LIMIT 1");
    }
}

看起来控制器和模型混合起来。然而在逻辑方面,项目是一个容纳projectinfo的容器,它可以是文本,图像和视频。

It seems controllers and models gets mixed up this way. Yet in a logical way a project is a container that holds projectinfo, which could be text, images and maybe video's. How would I go about to set that up logically as well.

推荐答案

原始问题



第一部分,关于URL是什么叫:路由或调度。在与Symfony 2.x的关系中有很好的文章,但它背后的想法是什么重要。

The original question

The first part, about the URLs is something called: Routing or Dispatching. There is quite good article about it in relationship with Symfony 2.x, but the the idea behind it is what matters. Also, you might looks at ways how other frameworks implement it.

对于原始URL示例,库将存储在DB中。不是吗?他们会有唯一 ID。这使得这个, / backend / projects / edit / 5 / gallery / 2 毫无意义。您的网址应该看起来更像:

As for your original URL examples, galleries will be stored in DB. Won't they? And they will have a unique ID. Which makes this, /backend/projects/edit/5/gallery/2 quite pointless. Instead your URL should look more like:

/backend/gallery/5/edit         // edit gallery with ID 5
/backend/project/3              // view project with ID 3
/backend/galleries/project/4    // list galleries filtered by project with ID 4

网址只应包含您真正需要的信息。

The URL should contain only the information you really need.

将指示3个控制器:


  1. 单一图库管理

  2. 单项目管理

  3. 处理图库列表

并且示例网址的模式类似于:

And the example URLs would have pattern similar to this:

/backend(/:controller(/:id|:page)(/:action(/:parameter)))

其中 / backend 部分是必需的, > controller 是可选的。如果找到控制器,则 id (或页面,当您处理列表时)和 action 是可选的。如果找到操作,则附加的参数是可选的。

Where the /backend part is mandatory, but the controller is optional. If controller is found , then id ( or page, when you deal with lists ) and action is optional. If action is found, additional parameter is optional. This structure would let you deal with majority of your routes, if written as a regular expression.

在开始使用或编写某种PHP框架之前,您应该学习如何编写适当的面向对象的代码。这并不意味着知道如何写一个类。它意味着你必须真正理解,什么是面向对象的编程,它基于什么原则,人们犯了什么常见错误,以及什么是最普遍的误解。下面几个讲座可能会帮助你:

Before you start in on using or writing some sort of PHP framework, you should learn how to write proper object oriented code. And that does not mean "know how to write a class". It means, that you have to actually understand, what is object oriented programming, what principles it is based on, what common mistakes people make and what are the most prevalent misconceptions. Here are few lecture that might help you with it:

  • Inheritance, Polymorphism, & Testing
  • Advanced OO Patterns (slides)
  • Unit Testing
  • The Principles of Agile Design
  • Global State and Singletons
  • Don't Look For Things!
  • Beyond Frameworks (slide)
  • Agility and Quality (slides)
  • Clean Code I: Arguments
  • Clean Code III: Functions

这应该给你一些概述主题..是的,很多。但怀疑你会喜欢的视频比书。否则,一些阅读材料:

This should give you some overview of the subject .. yeah, its a lot. But is suspect that you will prefer videos over books. Otherwise, some reading materials:

  • PHP Object-Oriented Solutions
  • Design Patterns Explained
  • Patterns of Enterprise Application Architecture

你会注意到很多材料都是语言不可知的。这是因为基于类的面向对象语言的理论是一样的。

You will notice that a lot of materials are language-agnostic. That's because the theory, for class-based object oriented languages, is the same.

在代码中使用 extends 关键字。这表示。没关系,如果 class Oak extends Tree ,因为所有的橡树都是树。但是如果你有 class User extends Database ,有人可能会冒犯。实际上有一个OOP原则,谈论它:利斯科夫替代原则 ..也有一个非常短暂解释

Be careful with extends keyword in your code. It means "is a". It is OK, if class Oak extends Tree, because all oaks are trees. But if you have class User extends Database, someone might get offended. There is actually an OOP principle which talks about it: Liskov substitution principle .. also there is a very short explanation

这篇关于PHP OOP核心框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆