从开发到生产的MVC架构

时间:2023-01-16 09:48:33

I am trying to gain a better understanding of MVC architecture. I write both front end and back end web applications using LAMP stack. I don't use any frameworks yet. I code in Notepad++ and use WinSCP for file transfer (Putty for terminal). All of the server side coding is done in PHP.

我正在努力更好地理解MVC架构。我使用LAMP堆栈编写前端和后端Web应用程序。我还没有使用任何框架。我在Notepad ++中编码并使用WinSCP进行文件传输(Putty for terminal)。所有服务器端编码都是用PHP完成的。

Based on the reading I've done, this is my best guess on how MVC's files are structured:

基于我所做的阅读,这是我对MVC文件结构的最佳猜测:

+ -- Project1
|    + -- models
|    |    + -- model.php
|    + -- views
|    |    + -- view.php
|    + -- controllers
|    |    + -- controller.php
+ -- Project2
|    + -- models
|    |    + -- model.php
|    + -- views
|    |    + -- view.php
|    + -- controllers
|    |    + -- controller.php

So, while there are a lot of articles and blogs about what models, views, and controllers are, I haven't seen anything about how to set this up past what I've gathered above.

因此,虽然有很多关于模型,视图和控制器的文章和博客,但我还没有看到任何关于如何设置上述内容的内容。

I have no idea how this becomes integrated in relation to my public folder on my server. Here is my guess, assuming that /var/www/html/ is my public directory.

我不知道这是如何与我服务器上的公共文件夹相关联的。假设/ var / www / html /是我的公共目录,这是我的猜测。

+ -- var
|    + -- www
|    |    + -- html
|    |    |    + -- my-application-1.php
|    |    |    |
|    |    |    + -- my-application-2.php
|    |    + -- Project1
|    |    |    + -- models
|    |    |    |    + -- model.php
|    |    |    + -- views
|    |    |    |    + -- view.php
|    |    |    + -- controllers
|    |    |    |    + -- controller.php
|    |    + -- Project2
|    |    |    + -- models
|    |    |    |    + -- model.php
|    |    |    + -- views
|    |    |    |    + -- view.php
|    |    |    + -- controllers
|    |    |    |    + -- controller.php

I am assuming based on what I've read (maybe I'm wrong) that people put their entire project "behind" their public folder. I am also assuming that you take your final application file (my-application1.php or my-application-2.php), put it in your public folder for people on the internet to use, and reference (php's include/require) your models, views and controllers from that application file. I've never referenced files behind my public file, but I'm assuming a lot here.

我假设基于我所读过的(也许我错了)人们将他们的整个项目“置于”他们的公共文件夹之后。我还假设您将最终的应用程序文件(my-application1.php或my-application-2.php)放入您的公共文件夹*互联网上的人使用,并参考(php的include / require)您的该应用程序文件中的模型,视图和控制器。我从来没有在我的公共文件后面引用文件,但我在这里假设很多。

Can someone verify any of this for me? How is this supposed to be set up in relation to the rest of my linux server? Is there anything I am assuming that is incorrect that you'd care to correct?

有人可以为我验证这些吗?这应该如何设置与我的Linux服务器的其余部分相关?我有什么假设是不正确的,你需要纠正吗?

I am trying to gain a good understanding of this before I pick up any frameworks (probably leaning toward Zend, Laravel, or CakePHP). I have read that frameworks help with this, but I currently don't have the resources to set up a framework and I would like to gain a full understanding of this before trying to pitch this to my team.

在我拿起任何框架(可能倾向于Zend,Laravel或CakePHP)之前,我试图对此有一个很好的理解。我已经读过框架对此有所帮助,但是我目前没有资源来设置框架,我想在尝试向我的团队推销这个框架之前充分了解这个框架。

2 个解决方案

#1


You're saying in your question that you don't have the resources to set up a framework but yet you have to resources to build one from scratch without even having the slightest idea what you're doing?

你在问题中说你没有足够的资源建立一个框架,但是你需要资源从头开始构建一个框架,而你甚至不知道你在做什么?

Believe me, you don't want to do it yourself. At the very least, you should first try some existing framework like symfony2 or zf2 or whatever you like.

相信我,你不想自己做。至少,你应该首先尝试一些现有的框架,如symfony2或zf2或任何你喜欢的。

When you'll become familiar with some of these and understand how they work, you will be able to create your own and give up in front of the tons of work it require.

当您熟悉其中的一些并了解它们的工作原理时,您将能够创建自己的并放弃它所需的大量工作。

Anyway, to answer the question, here's how zend framework 1 is structured

无论如何,要回答这个问题,这里是zend框架1的结构

project
|-- application
|   |-- Bootstrap.php
|   |-- configs
|   |   `-- application.ini
|   |-- controllers
|   |   |-- ErrorController.php
|   |   `-- IndexController.php
|   |-- models
|   `-- views
|       |-- helpers
|       `-- scripts
|           |-- error
|           |   `-- error.phtml
|           `-- index
|               `-- index.phtml
|-- library
|-- public
|   |-- .htaccess
|   `-- index.php

With this framework, you would setup your web root to /path/to/project/public. As you can see, none of the application code is directly accessible from the web. All incoming requests come throught the index.php where all the magic is.

使用此框架,您可以将Web根目录设置为/ path / to / project / public。如您所见,没有任何应用程序代码可以直接从Web访问。所有传入的请求都来自index.php所有的魔法。

If you're interested in knowing what the magic really is, all the php web framework are open source so feel free to check it out for yourself :

如果你有兴趣了解真正的神奇之处,那么所有的php web框架都是开源的,所以请随意查看:

ZF2 on github

github上的ZF2

Sf2 on guthub

在guthub上的Sf2

#2


It's very dependant on the framework you choose to use, but the general idea is that the actual source directory is NOT stored in the web root. I use /web/projects on my deployments, then symlinking the public folder out to the htdocs

它非常依赖于您选择使用的框架,但一般的想法是实际的源目录不存储在Web根目录中。我在部署中使用/ web / projects,然后将公共文件夹符号链接到htdocs

> cd /var/www/html
> ln -s /web/projects/myProject/public myproject

You'll then usually have an index.php file in the public file that kicks things off. There's also usually a .htaccess file that intercepts all requests and points them at that file.

然后,您通常会在公共文件中放置一个index.php文件来解决问题。通常还有.htaccess文件拦截所有请求并将它们指向该文件。

Best option is to choose a framework (Laravel and Symfony are excellent options) and follow the getting started guides.

最好的选择是选择一个框架(Laravel和Symfony是很好的选择)并遵循入门指南。

#1


You're saying in your question that you don't have the resources to set up a framework but yet you have to resources to build one from scratch without even having the slightest idea what you're doing?

你在问题中说你没有足够的资源建立一个框架,但是你需要资源从头开始构建一个框架,而你甚至不知道你在做什么?

Believe me, you don't want to do it yourself. At the very least, you should first try some existing framework like symfony2 or zf2 or whatever you like.

相信我,你不想自己做。至少,你应该首先尝试一些现有的框架,如symfony2或zf2或任何你喜欢的。

When you'll become familiar with some of these and understand how they work, you will be able to create your own and give up in front of the tons of work it require.

当您熟悉其中的一些并了解它们的工作原理时,您将能够创建自己的并放弃它所需的大量工作。

Anyway, to answer the question, here's how zend framework 1 is structured

无论如何,要回答这个问题,这里是zend框架1的结构

project
|-- application
|   |-- Bootstrap.php
|   |-- configs
|   |   `-- application.ini
|   |-- controllers
|   |   |-- ErrorController.php
|   |   `-- IndexController.php
|   |-- models
|   `-- views
|       |-- helpers
|       `-- scripts
|           |-- error
|           |   `-- error.phtml
|           `-- index
|               `-- index.phtml
|-- library
|-- public
|   |-- .htaccess
|   `-- index.php

With this framework, you would setup your web root to /path/to/project/public. As you can see, none of the application code is directly accessible from the web. All incoming requests come throught the index.php where all the magic is.

使用此框架,您可以将Web根目录设置为/ path / to / project / public。如您所见,没有任何应用程序代码可以直接从Web访问。所有传入的请求都来自index.php所有的魔法。

If you're interested in knowing what the magic really is, all the php web framework are open source so feel free to check it out for yourself :

如果你有兴趣了解真正的神奇之处,那么所有的php web框架都是开源的,所以请随意查看:

ZF2 on github

github上的ZF2

Sf2 on guthub

在guthub上的Sf2

#2


It's very dependant on the framework you choose to use, but the general idea is that the actual source directory is NOT stored in the web root. I use /web/projects on my deployments, then symlinking the public folder out to the htdocs

它非常依赖于您选择使用的框架,但一般的想法是实际的源目录不存储在Web根目录中。我在部署中使用/ web / projects,然后将公共文件夹符号链接到htdocs

> cd /var/www/html
> ln -s /web/projects/myProject/public myproject

You'll then usually have an index.php file in the public file that kicks things off. There's also usually a .htaccess file that intercepts all requests and points them at that file.

然后,您通常会在公共文件中放置一个index.php文件来解决问题。通常还有.htaccess文件拦截所有请求并将它们指向该文件。

Best option is to choose a framework (Laravel and Symfony are excellent options) and follow the getting started guides.

最好的选择是选择一个框架(Laravel和Symfony是很好的选择)并遵循入门指南。