如何在我的PHP / SQL / HTML / CSS代码实现MVC样式? [英] How to implement MVC style on my PHP/SQL/HTML/CSS code?

查看:195
本文介绍了如何在我的PHP / SQL / HTML / CSS代码实现MVC样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个用于可视化一些数据的程序。我的程序从MySQL数据库获取特定的输入,并绘制一些图表(libchart库),创建一些表等。



我的问题是,现在它的代码地狱。我有大约7 php文件(索引,图形页面,画廊等)与HTML / CSS和PHP / SQL代码在一起(其中一些只是有php扩展,但只有HTML里面)。我暂时没有阅读和理解项目的问题,但我想如果有人试图,他可能会头痛。此外,像这样的继续编程是不切实际的,因为该项目在未来可能不容易扩展。



你有什么建议如何成功地分离HTML / CSS PHP / SQL?我不想使用框架,因为我不做任何需要用户输入,会话处理等。我只是运行一些查询和可视化的结果。我在这里主要谈论建筑,如果适用也许一个脚本帮助我(我已经阅读有关Smarty,但我不知道这是否是我需要的)。

解决方案


你有什么建议如何成功地从PHP / SQL分离HTML / CSS?


恭喜您了解如何改进代码。这是前提条件,你需要改善它,主题是漫长的。所以你的意志是至关重要的。



我开始轻松,然后尝试给一些提示。因为你缺少经验,寻找一个点开始,最肯定的是下面的列表中的最后一个。但第一件事:第一件事:



要将某些东西彼此分开,您需要一些代码:

  [HTML / CSS / PHP / SQL] 

[HTML / CSS]< - > [分离器] [PHP / SQL]

如果你只看到 PHP / SQL



通常在程序中传递获得处理的数据。数据是非常动态的,可能有复杂的复杂性,特别是如果你将数据传递到一个应该格式化的输出例程。



有多种方式如何可以写入分隔符(或其中的多个)。您可以 区域中例如。您有一个数据库层或数据库组件,用于关注与数据库的交互。





简单来说,这就是软件设计的面食理论:一个模板引擎




  • 意大利面料代码 - 所有代码都是交织在一起的,最好与Bolognese或Aglio,Olio e Peperoncino

  • Lasagne代码 - 分层,一个图层还有两个与其互动的图层(除非底部或顶部),总是使用Béchamel酱。

  • Tortelini代码 - 小部件只是做他们的工作,他们里面有肉类或辣蔬菜。



像我们在我们的生活中吃不同的面食,编程时,我们需要处理所有这些不同类型的代码,我们开发我们自己喜欢的味道随着时间的推移。作为一个孩子,我们饲料,但随着时间的推移,我们开始做自己的东西,并改变食谱。



所以我认为这是一个好点,你只是不想现在就可以在接下来的几周里使用MVC框架X了,因为有人告诉你这是现在吃的方式。在吃之前,有品尝,对吧?更不要说快餐,你知道像这些面条酱中包 - 只加水。 Urgh。



我不知道你的输出需要什么数据,输入是什么。以下是一些用于输出HTML / CSS和与MySQL数据库交互的应用程序的粗略重构提示。这不能是一个完整的列表,并且描述只能粗略地概述一些想法:




  • 将CSS移出HTML 。在链接的CSS定义中有效使用选择器,并替换任何 style 属性(如果还有一些)。这使得您的CSS代码可重用和更加模块化。它将帮助您在HTML中找到缺陷,并将结构(HTML)与演示文稿(CSS)分离。有效的HTML从CSS的有效使用开始,这两个非常强大的在一起,并且经常已经减轻了您的程序输出例程。

  • 将业务逻辑移出HTML 。 HTML和你的代码可以是一个野兽,所以更好地保持它们分开。他们有相互斗争的倾向,因为两者都非常强大,所以当你开发你的应用程序时,战斗将继续,这会分散你从你需要做的工作。

    考虑如果你需要已经在你的应用程序中有复杂的输出,或者如果你可以传递带有子元素的数组(一个键是一个var,一个var可以包含一个字符串或数字或另一个var数组)。通常,将复杂数据传递到视图模板中都需要。你的HTML然后只需要回显一些数组成员和 foreach 子阵列。这是一个非常简单的技术来创建模板。你可以使用PHP,所以你实际上是灵活的(只是绘制边框的代码属于你的视图层,它是应用程序的一部分,例如为视图提供值)。

  • 将SQL移出您的代码。移动数据库交互代码。创建一个或多个对象,这些对象具有以您在实际处理代码中需要(消费)的方式返回数据的方法,例如 $ component-> getThatData()然后以标准化形式返回数据。然后使这些组件使用专用的数据库组件与数据库通信。在您的应用程序代码(业务逻辑)中,只使用数据库组件,最好使用创建的对象来获取数据,因此您的主代码中不再有任何SQL行。

  • 分割并征服您的应用程序代码:将您的代码分为 事务脚本 。它们通常很容易从现有的意大利面条代码创建,并且可能成为您在中间名称中查找的所述分隔符

  • 使用清楚的语言:如果您有复杂的格式化字符串数据这是不规范化,写自己的解析器类,为你做的工作,并可以很容易地重用(如果在你的应用程序的情况下)。由于您应该期待在代码中最小化使用纯SQL,您还应该期待将复杂的正则表达式移除。 封装变化的是关键点。这同样适用于长例程,只处理一些数据(例如,排序,顺序和以另一种格式排序),将它们移动到每个组件中,并考虑如何使它们可访问和可重用。

  • 使您的代码运行:了解您如何调用程序中的功能的逻辑。您可以尝试将功能从调用方式中分离出来。例如。一些调用任何事务脚本的例程。如果您通过浏览器直接请求PHP文件,这可能不是必需的,因为那些是您的事务脚本,并且Web服务器小心地解决通过URL发送到您的应用程序到事务脚本的命令。但是,您应该将任何用于处理传入命令及其参数的逻辑封装为可重用的组件(例如,包含标准代码的 类,以获取URL和
  • 通过在浏览器调用的所有文件的最顶部包含相同的文件, 创建公共入口点。然后,您可以将常用代码(如设置应用程序会话状态对象和初始化数据库组件)放入其中,以及 应用程序控制器
  • 通过查找逐字复制的代码来删除重复。将它包装到一个函数或类中。为您自己的应用程序创建一个库文件夹,您可以在其中放入包含。如果你使用类名和命名空间遵循一个共同的模式,你可以很容易地使用自动加载器,使包容容易。除了第三方代码之外,还可将 您的图书馆。

  • 使用轻量级现有组件。轻量级是重要的因为你已经有你自己的代码,你不想转身,把它所有的一次到一个框架。
    现有的是很重要的,因为你不想重新发明轮子。你将有足够的工作来自己重构你的代码。在你感觉到你的应用程序更好,你仍然有力量和意志,你可以总是写一切新的。但是,如果你是孤身或在一个小团队,现有是相当强大。
    简单的库是例如:

  • 创建应用程序状态,例如作为一个对象,你可以使用在一些组件需要知道应用程序状态或彼此没有直接交互的情况下。默认情况下,如果没有PHP,则使用全局和全局静态变量来创建状态。然而,这些变量可以使你的生活困难的代码增长。当创建应用程序状态对象时,清楚哪些组件使用它,对它的访问可以被控制(例如调用方法而不是读取变量,这有助于调试)和组件以及可以找出它是否正确的时间在应用程序流中开始起作用。

  • 保留工作应用程式,让您的程式码保持运行状态。理想情况下,这将由自动测试备份。考虑你需要重写很多。例如,如果您开始集成数据库组件,请执行此操作。将所有现有代码移动到一步。所以谁告诉你它仍然运行?使用git更好地撤消和测试的东西。这比选择正确的库更重要。保存工作应用程序也始终是关键点,因为这是为什么您更改它,对吧?


I have been developing a program for the visualization of some data. My program takes specific input from a MySQL database and draws some graphs (libchart library), creates some tables etc.

My problem is that right now its a code hell in there. I have around 7 php files (index, graph-page, gallery, etc) with HTML/CSS and PHP/SQL code all together (some of them just have the php extension but have only HTML inside). I have no problem to read and understand the project for the time being, but I guess if someone else tried to, he might get a headache. Plus, continuing programming like this is not practical because the project might not be easily scalable in the future.

Do you have any suggestion on how to successfully seperate HTML/CSS from PHP/SQL? I don't want to use a framework since I'm not doing anything that requires user-input, session handling, etc. I just run some queries and visualize the results. I'm mostly talking about architecture here, and if applicable perhaps a script to help me (I've read about Smarty but I'm not sure if that's what I need).

解决方案

Do you have any suggestion on how to successfully seperate HTML/CSS from PHP/SQL?

Congrats for looking how you can improve code. That's the precondition, you need to want to improve it and the topic is lengthy. So your will is crucial.

I start lightly and then try to give some tips. As you're missing experience, look for one point to start with, most certainly the last one of the list below. But first things first:

To separate something from each other, you need to have some code that separates:

[HTML/CSS/PHP/SQL]

[HTML/CSS] <--> [SEPARATOR] <--> [PHP/SQL]

The Separator here actually is PHP code as well, but I think you get the idea.

As you can see only the Separator talks with HTML/CSS and PHP/SQL.

So both HTML/CSS and PHP/SQL need to have an interface with Separator (the line between) to make this work.

Normally in a program you pass around data that get's processed. The data is pretty dynamic and can have a compound complexity, especially if you pass data to an output routine that should format it properly.

There are multiple ways of how such a Separator (or multiple of them) can be written. You can layer your software or provide components that do things in their area or domain. E.g. you have a database layer or database component that takes care about the interaction with the database.

Or you have a templating engine that takes care to put your strings and arrays into some readable HTML.

In short this is the pasta theory of software design:

  • Spaghetti code - all in one, code is heavily interwoven, preferable with Bolognese or Aglio, Olio e Peperoncino.
  • Lasagne code - Layered, one layer has two other layers it interacts with (unless bottom or top), always with Béchamel sauce.
  • Tortelini code - Small components that just do their job, they have Meat or Spicy Vegetables inside.

Like we eat different pasta in our lives, when programming we need to deal with all these different type of code as well, and we develop our own preferred taste over time. As a kid we're feed but over time we start to cook something our own and vary the recipes.

So I think it's a good point you just don't want to now eat MVC Framework X with much awesome for the next weeks only because somebody told you it's the way to eat now. And before eating, there is tasting, right? Not to mention fast-food, you know like these noodles with sauce in package - only add water. Urgh.

I don't know which data your output needs and what the input is. Following are some rough refactoring tips for applications that output HTML/CSS and interact with a MySQL database. This can not be a complete list and the descriptions can only roughly outline some thoughts:

  • Move CSS out of HTML. Use selectors effectively in the linked CSS definition and replace any style attributes if you still have some. This makes your CSS code re-useable and more modular. It will help you to find flaws inside your HTML and to separate the Structure (HTML) from the Presentation (CSS). Effective HTML start with effective usage of CSS, those two are very powerful together and often this already will lighten your programs output routines.
  • Move business logic out of HTML. Both HTML and your code can be a beast, so better keep them apart. They have the tendency to fight with each other, and as both are very powerful, the fight will continue while you develop your application, that distracts you from the work you need to do.
    Consider if you need to already have complex output inside your application or if you can just pass on arrays with subelements (a key is a var, a var can contain a string or number or another var-array). Normally that is all needed to pass even complex data into a view or template. You HTML then only needs to echo some array members and or foreach over subarrays. This is a very simple technique to create a template. You can use PHP for it, so you're actually really flexible (just draw the border which code belongs into your view layer and which is part of the application, e.g. providing values for the view).
  • Move SQL out of your code. Move the database interaction code away. Create yourself one or multiple objects that have methods which return the data in a way you need (consume) it in your actual processing code, like $component->getThatData() which then returns data in a normalized form. Make those components then use a dedicated database component to talk over with the database. In your application code (business logic) only use the database component and preferably the objects you create to get the data, so you don't have any line of SQL any longer inside your main code.
  • Divide and Conquer your application code: Divide your code into Transaction Scripts. They are often easy to create from existing spaghetti code and will be probably become the said Separator you're looking for in middle terms. They will then have the role to process data and passing it on (into the output/view).
  • Use clear language: If you have complex formatted string data that is not normalized, write yourself Parser classes that do the work for you and which can be easily re-used (if that's the case in your application). As you should look forward to minimize the use of plain SQL in your code, you should also look forward to move complex regular expressions away as well. Encapsulate what varies is a key point. Same applies to long routines to just handle some data (e.g. sort, order and arange it in another format), move them into components of each own and think about how you can make them accessible and re-useable.
  • Make your code functioning: Find out about the logic how you invoke functionality in your program. You can try to separate functionality away from how it's invoked. E.g. some routine that invokes any of the Transaction Scripts. This might not be necessary if you request PHP files directly via the browser, as those are then your transaction scripts and the webserver takes care to resolve the command send via URL into your application to the transaction script. But you should then wrap any logic needed to process the incoming command and it's parameters into re-useable components (e.g. a Request class that contains standard code to get the URL and or variables from a HTTP request).
  • Create a common entry-point by including the same file at the very top of all files that are called via the browser. You can then put common code (like setting up the application session state object and initializing the database component) into it, see as well Application Controller
  • Remove duplication by looking for literally duplicated code. Wrap it into a function or class. Create a library folder for your own application into which you put your includes. If you follow a common pattern with Classnames and Namespacing, you can easily use an autoloader to keep inclusion easy. Make your library apart from third-party code. Place all third-party code into a library folder of it's own with one subdirectory for each third-party component.
  • Use lightweight, existing components. Lightweight is important because you have your own code already, you don't want to turn and press it all at once onto a framework. Existing is important because you don't want to re-invent the wheel. You will have enough work for your own refactoring your code. After you feel better about your application and you still have power and will, you can always write everything new. But if you're alone or in a small team, Existing is pretty powerful. Simple libraries are for example:
  • Create Application State, e.g. as an object you can make use of in case some components need to know about the application state or each other without direct interaction. By default in PHP if you don't have one, global and global static variables are used to create state. However these variables can make your live hard as code grows. When you create an application state object, it's clear which components make use of it, access to it can be controlled (e.g. calling a method instead of reading a variable, which can help with debugging) and components as well can find out if it's the right time in the application flow to come into action. It's also a good tool for refactoring your code over time.
  • Preserve a Working Application, keep your code in a state to run. Ideally this would be backed up by automatic tests. Consider that you need to rewrite much. For example if you start to integrate a database component, do it. Move all your existing code to it in one step. So who tells you it still runs? Use git for better undo and to test stuff. That's more important than choosing the right library. Preserving a working application is also always the key point because that's why you change it, right?

这篇关于如何在我的PHP / SQL / HTML / CSS代码实现MVC样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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