Why should I use MVC in a php website? [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Many frameworks are based on mvc, but I have no idea about MVC. So what is the use of MVC?
Is it for speed, security or any other reason?

MVC allows you to separate your business logic from your presentation layer. This "Separation of Concerns" allows you to quickly find and edit portions of your code. It also enables easy reuse of your UI components across your system.
Check out the wiki page for a overly academic and technical introduction to MVC http://en.wikipedia.org/wiki/Model_view_controller

MVC is mostly for better maintainability of your code. By separating the database logic from the presentational logic from the controller logic you can make changes/rewrites/maintence more easily.

it also solves the problem of "spaghetti code", you can outsorce your HTML/XML/PDF/XSL creation Code to your View/Template engine, get the Data from Your Model(DB/File/RemoteCall,...) and your Controller controls the behaviour of Both, you can also simply exchange the View/Models without even Change the Controller if u implement it right, so you gain Seperation of Concerncs, get better Code & Maintainability and can easily swap components
also its easier to manage if your projects grow. I recommend the Usage of a FrontController which selects the right Controller for you depending on the Users input, you can also use Inversion of Control/DependencyInjection Pattern there and let your Controller be configued by your FrontController / Pass DB Connection and lots of lots of lots more funny stuff
Now u got a simple application framwork:) Use Zend Instead:)

Related

Easy way to locate related files in PHP project [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am a core PHP developer and learning MVC Frameworks: CodeIgnitor and Kohona. I downloaded few sample projects to do research on how they work. I am really confused how to determine which Model, which View and which Controller are interrelated to each other.
How can I easily locate which View is for with Controller and vice versa for models, views and controllers.
Also, can anyone tell me what's the major advantage of using these frameworks (Sorry this question to be asked in Google first, I am just looking for short answer) and I feel frameworks uncomfortable cause its hard to locate right file if someone else developed the application.
The right way to find related files in a project is that, you have to read code, for example, if you want to find what view or model is being used in a controller then you should seek in the ode, which model it calls and which view it loads. This is the only way to find related files.
There are lots of advantages to use a framework but one of the most important advantages of using a framework is that, it speeds up development process, writer less do more, it makes your development process faster because it provides you some tools, for example, a Session class for session management, a Validation class for user input validation etc, using these tools you can easily make an application with a very few code and it saves your time so you become more productive.
Another important advantage is that, if you are working in a team and use a framework then each developer knows the code organization of the project because the framework ties everyone to a single set of rules that every developer must follow, everyone follows the same conventions so there is no confusions and any time you may add more developer to work on a project who knows the same framework and can work without any problem.
CodeIgniter and Kohana does not implement MVC (even if they say so...).
A typical flow in php frameworks that call themself MVC are : A url routes to a controller, the controller retrives data from model and passes it the view.
The advantage to using a framework should be that it provides a set of tools for rapidly development, making you focus on the unique code for your application.
The documentation for each framework should be pretty straight forward, you should read it if you going to use a framework...

PHP generating html [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am new to PHP, coming from ASP.net background, the most trouble I am having is generating html from PHP. My question is how is it done in industry? I see 4 ways:
generate from inside PHP program.
use PHP "helper classes" to do that.
mix PHP from inside HTML.
use templates.
Are there more ways, and which one is used on professional projects. If it number 2 or 4, could you suggest "best" (most used) helper classes, or templates.
Use an MVC framework like CodeIgniter, and follow the standards there. Try to keep your business and display logic as separate as possible!
The reasoning behind this is that if you decide later on to modify your code, it's easier to find the code in question. It's simpler to search through a few template files than it is to pick through all your logic to look for one snippet of html.
Most professional development is now done using an MVC framework, such as CakePHP or CodeIgniter.
You could also look at a standalone templating system like Smarty.
When you start with php, you find several ways to generate your webpages.
Html is a template language, so it is just formatted string, and php is very good generating string, so you have a lot of ways to do it.
But some ways are better than others.
For example: If you mix your html code with your php code, you get a very difficut to read, mantain and scale code, so it is better if you separate the code in layers. MVC is a design pattern that handles very good this kind of layer abstraction, so read a little about it.
Inside PHP, you could find several tools to do it at the good way, like using a framework for your project. The frameworks make easy and quick your development and manage the mvc abstraction well.
I recommend Laravel, it is easy to use and is so powerful, that you will do your job really fast.
But if you don't want to use a framework, you still could separate your logic from the html code, and fill the html only whe you need. Look at The following class, maybe it could be useful to you.

Benefits of using cakePHP? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I know PHP well. But i haven't tried cakePHP before and i was willing to learn cakePHP. So just want to know that is there any benefits of using cakePHP over PHP?
Thanks,
aby
cakePHP, like CodeIgniter, FuelPHP and Symfony (just to name a few) are frameworks. What this means is that they have tried to abstract some of the normal, every-day things you do in development in an effort to speed up development time and make you more profitable.
In today's world, there is no excuse for not using a framework. If you refuse to use a framework, you find yourself creating a lot of the things that a good framework provides anyway (i.e. data abstraction layers, session classes, form validation, etc). By using a framework, you not only gain time, but you gain stability as the different components of the framework should be designed to work well together and have been thoroughly tested by the community.
Whichever framework you choose is up to you and hopefully now you understand it's benefits.
CakePHP is an MVC framework that will allow you to setup your data model and connect it to a database to be easily manipulated in PHP. It also allows you separate your business logic from your display logic using a controller.
It also gives you a lot of useful functionality like authorization, sessions, form validation, pagination.. for free. You should check out the feature list.
If you didn't use CakePHP you would have to roll your own framework, which is a lot of extra hassle and work. There are of course other frameworks.
The answer is pretty simple, it comes down to your need's, do you need a framework?
You say that you know php well, i doubt you know it as well as you think as you would not ask a question of this nature, you would understand the concepts behind a framework.
You say:
So just want to know that is there any benefits of using cakePHP over PHP?
it's not a language sir, it's a framework that supplies the PHP foundations for an application, why would you not wan't to use such a thing, what you should be asking is:
What's the best framework to learn once you have mastered PHP Fundamentals.
But don't ask it here, there are plenty of posts that answer that question for you.
CakePHP is an MVC framework But to simply say you need to design the data models and relationship (creating necessary tables) before start the work , once you use bake command 70% of coding are given

in MVC, where do you draw the line between a controller and model? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I've seen code written where almost all non-route related code is passed to a model. I have also seen code where all database persistence is handled by a model, but non-DB processing is handled by the controller.
Which is the better approach?
The line between controller and model is actually quite clear.
Model is your application's heart. It contains the business/domain logic required to solve the problem your application was written for. The Model is usually layered into several other layers, e.g. persistence, services, domain, etc. It's a common misconception that the Model is just the database, as much as it is a common misconception that the database should be an ActiveRecord.
The controller (and the view) are part of the presentation layer. A controller's sole responsibility is to receive and handle user input directed towards your application and delegate this to the appropriate parts in the model. Nothing more. It should not handle complex application flow or code of your problem domain. You want controllers to be skinny and models fat with logic. The Model should not know about either C or V and you should be able to swap out V and C for a different presentation layer without having to touch your M.
See MVC Excerpt in Patterns of Enterprise Application Architecture

Lightweight MVC - no framework [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Hi sorry if this is a naive question, but what did people do before mvc frameworks became so popular? All you hear of nowadays, and im talking php here, are mvc, Zend etc but what did developers do beforehand?
Are there some developers who use the mvc pattern but without a framework - if so how do they do this and is it really complicated to set up?
MVC is a design pattern. You can easily roll your own MVC "framework" (technically even without using object-oriented programming). The main goal is simply to have a separation between data storage, business logic, and presentation.
When I was first learning about MVC, I decided that trying to sift through the mountains of code of CakePHP or other frameworks was simply too complicated. I started writing my own "framework" using this tutorial (http://fuelyourcoding.com/php-frameworks-just-roll-your-own-part-1/). It's really not as much work as you think (you can go through that tutorial in a day and have a very nice mini-MVC), and you can expand it later into a full-fledged framework later on if you have the time and dedication.
As to the question of what developers did before frameworks, well, they just wrote everything themselves. Unfortunately this led to a lot of spaghetti code with HTML mixed with PHP blocks and SQL statements, but that's not really a fault with not having a framework, rather with not trying to implement any sort of separation of concerns.
Edit: Part 2 is probably the most important part because it shows you how to make a template. This isn't actually the exact tutorial, the one I used seems to have disappeared.

Categories