File Structures -- Why use (Zend) MVC? - php

I'm relatively new to PHP and have been writing a project using what I believe is a fairly basic file architecture - all files and sub-folders within one main site folder accessed separately. Within the project, I've been using the Zend Framework as more of a library than an actual framework. I'm happy with Zend and using it like this makes things very easy. However, I have recently been starting to dread the maintenance and expansion that may be required with the project (something I should have thought about before, I know). I have been doing a bit of research and looking into using the Zend MVC rather than the basic structure as it appears this would help to remedy that fear. However, it would mean a lot of backtracking in the project (not a huge deal, just a little frustrating).
What I'm wondering is if there are any other reasons why I should take the time to switch away from the basic structure over to an MVC architecture, or if there are any other file structures I should be considering?

The first reason is that MVC has been shown to be effective in being able to maintain your code.
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
The second reason, which I believe is part of the first one, is that it allows developers to come in behind you and already know what the basics are. Instead of searching through your project and trying to find out how it works, they already know.
I might suggest Kohana as a good framework to use. http://kohanaphp.com

Zend is very good Framework. with a lot of classes.
IMHO: But it's more Class library, than Framweork.

principe of MVC is very useful. [C]ontroller gets data from the datebase named [M]odel, changes it, and outputs everything to the [V]iew.
In cakephp, if you know which action do you need to edit, you surely know where to search it, to open and edit. it's about Structure.
I might suggest Cakephp. http://www.cakephp.org

Related

Joomla like component architecture.. Starting from scratch

Blockquote
Edited:
http://blog.fedecarg.com/2008/06/28/a-modular-approach-to-web-development/
The above approach is what I'm looking for. So are there php frameworks available which will allow me to create modular structure like above for my code ?
Edited:
Blockquote
I would like to know which is the current best php framework which is 100% modular like joomla component architecture.
In joomla all you have to do is upload a set of files under the "components" directory and you can add "any" kind of complex functionality to joomla.
I want to develop php applications from scratch and I want all of my applications to have the same ease of joomla's component architecture. So Im currently looking for a php framework.
So which is the best way to go about if I want to do the above ?
Should I choose a php framework like codeignitor or zend etc ? but you see, even if is use them, Suppose if I have to create a new function I have to upload files to atleast 3 directories i.e controller, model and view. But you see in joomla all I have to do is upload all the files of course suitably structured into just a single directory called "components" and the rest is taken care of automatically
So what is the best way to go forward ?
Shanthi
Joomla is a CMS system AFAIK so you cant really compare a PHP framework to it.
In terms of modularity Zend Framework has an excellent excellent, simple to implement module structure.
You sat that to add complex functionality to joomla you upload files in the correct structure and joomla does it for you, well as long as you create your module files in the right structure for ZF it will work too......
Your question is same as my question. but it was for 4 months ago.
I tried to find out how can i create such modular system. After some searching I understood that we should have a single directory like 'components' as you said, and put every data of modules in it. but how we should use them ?. Easy !. just create an handler (php file) that control your installation, uninstalling, positioning, settings. In fact i just solve 3 of them. positioning is so hard i think so, i never do it till now.
if you want an example of my explanation, just tell me to create it. I'll send it for you. :)
well i think you are comparing apples to exotic devil fruits or something.
you are adding modules to a cms and exepct the same from a famework.
it doesnt work like this.
the mvc structure you are talking about (model - view - controller) makes a lot of sense and helps you to keep your code organised and to keep up good class coherency.
its a design pattern which has evolved to a de facto standard and has been adopted by almost every framework.
zend framework is probably the best way to go for you.
its layouting engine and stuff is not as fast as one could wish, but it features execellent modularity. you can even partly overload core classes to suit your needs in your "module".
only abstract classes dont work (how should they?), thats why they are almonst never used.
if you really really wanna go with a very flexible excellent system and performance is not the most important point for you, you should get going with magento, which takes the modularity and flexibility even to a higher level by adding an xml structure to zend framework that allows you to organice all your pages, manage your dependencies, rewrite your model componenents etc.
but be a aware that the more you want the more performance you will loose. my personal fav from the frameworks out there is codeigniter, but i designed my own for my needs...
I agree that there is a difference as Joomla et al as CMS's and therefore come in at a higher level of functionality than a framework, but of course with less flexibility.
However you might want to explore Symfony 2 and Lithium (Cake 3?). Both are designed to only use PHP 5.3 and greater and both use the namespace functionality in PHP. I have done less with Lithium but certainly in Symfony 2 everything (well nearly everything is a plugin making the framework modular and much easier to manage.
However you will never be able to compare the two Framework/CMS in exactly the same way as they are both built and designed for different purposes and with varying constraints.
N.B. Neither Symfony 2 or Lithium are production ready at present. Symfony is aiming at March 2011, not clear on Lithium's 1 dot oh date
[edit]
A number of answers here plump for Zend. What needs to be clear is that there are two types of framework (yes ok lot's and lot's) but being simplistic there are Full Stack and Glue.
Glue frameworks like Zend tend to be free and easy allowing you to use some parts and not others as you wish. So although nominally MVC you can easily implement Zend with V and C but not use M. You can write all your database calls manually in the controller etc.
Full Stack such as Symfony mean you pretty well use all of whether you like it or not. On the data side you use and ORM, Doctrine or Propel and a large amount of the framework is controlled from configuration files in YAML/XML. In this respect Symfony is very like Ruby et al. Please note this is not a recommendation or criticism of either.
Joomla in my opinion is gluey (but not a framework) but hey why use something that is 90% built and then rewrite 75% of it? Oh yes because the customer asks and your boss is unaware of the problems, i remember now.

How MVC framework will help to develop php based website

I used to develop websites using php . I like to learn some framework using php.
So I like to know how effective MVC is ?
Is this easy to learn ?
MVC is a way of organizing code that seems uniquely well suited for web applications. You'll have to organize your code in some particular way, try MVC and see if you like it. It's also the de-facto standard in web app design, so it makes your code easier to understand for other programmers.
It helps me minimize mixing languages -- views contain all the HTML, models all the SQL, and controllers describe and handle the API and support code (like authentication).
I have to say that when I first tried to get my head around MVC I had a great deal of difficulty (especially being someone who is self-taught and started with PHP). Put most simply, and most valuably for me, MVC is a good way to organize your code. It provides a template for separating the different layers of your application, which again sounds confusing, but actually isn't. (Again, this is meant to be an explanation of why MVC is useful and how to get started with it, not how it functions ... which I don't totally understand.)
First off, one really simple way to think about it is to compare it Wordpress (this is assuming you've played with Wordpress). Views function in much the same way as "themes": They are a simple way to combine presentation markup with whatever data is being pushed out to the page.
Models describe (and can interact with) the database.
Controllers do everything in between. (Calling functions in models, loading data into views.)
But it's also important to note that you can use MVC in any way you want to. While the idea is to get you to use a specific organizing pattern, no one is going to stop you from doing it in whatever way works best. I know quite a few folks who forgo models altogether and just use Controllers/Views to do everything they need. I found CodeIgniter to be pretty easy to get up to speed on, and now that I've got the hang of it I find it incredibly useful for both the functions the framework provides, but more importantly, the organizing that MVC forces me in to.
Hope that's helpful.
MVC is a very good design pattern for developing applications. It allows you clear separation between the views, the data access and processing logic which will result into more maintainable code.
Obviously learning MVC is going to be different for each person. My recommendations would be to read everything you can on MVC/PHP-MVC/PHP OOP that you can and then try to write your own framework. Then rewrite it using the things you learned the first time. Repeat.
Few of the major PHP frameworks implement MVC. Most use some variant of "Passive-MVC" or "Model-View-Presenter". They're following the concept, not the actual MVC pattern (which is a runtime organization for graphical apps, not for generating page output).
That's not to say the PHP interpretation of the concept doesn't bring any benefits. It's commonly believed to provide a better structure for large or growing web apps. It's less useful for implementing simple tasks.
I guess everything that was to be said about MVC was said already... so I'd like to point you to a good PHP framework to start with, should you decide to go with MVC - CodeIgniter. It's probably the easiest one to start with if you never used MVC before.
Just to add to all the wonderful answers given about MVCs, an MVC framework will do all the basic work for you and it helps save time. You'd be working on things custom to a particular project like its business logic intead of the basic Create, Read, Update and Delete functions.
#gowri
You can try with any php frame work. You are at initial stage so start learning codignator or cakephp. Both have good documentation and support. I recommend you codignator. Easy and good mvc.

Php framework to slowly refactor an inherited site

I've recently inherited a medium-sized php site which is horribly coded. It violates every best-practices methodology, from MVC to DRY, is vulnerable to SQL-injection and everything in between.
I've visited the other questions and already put everything on a VCS and am considering the framework alternatives. However I'd like your opinions on a framework that lets me slowly migrate from the actual site to a framework controlled one.
Thanks.
Zend Framework would actually be the best choice in my opinion, as it has a great use-at-will structure and it is no full-stack framework like most of the others.
That means that you can start with migrating the model-layer first without having to touch the view or controller part. And even when it comes to the controller part, you could first put everything into controllers without having to rely on the router, so you could still use your old URLs.
I will put my vote in for CakePHP (http://www.cakephp.org). It has the ability to manage everything very nicely.
Template
This will allow you to create the base template / layout for the site. It is the main body of the site. You can store multiple layouts all in the views/layouts directory. You can identify what layout you want to use for any given page within the site.
Static Content
If you have static content pages, they all reside in views/pages. These will load into the layout wherever you put the <?php echo $content_for_layout; ?>.
Custom Code
Many times, you will have custom code that may not fit in the framework. No worries, you can add this to the libs or vendors folders and call the functionality from there.
Speedy Upgrade Via Bake
One of the cool features of cake is the bake feature. Once you have added your schema to the database, you can use bake to have CakePHP write all of the models (with relationships), the controllers (with basic CRUD and admin sections), and the views for each action within the controller.
Cake has been a great fit for all of the projects I have worked on. It keeps the code well organized, has a very active community, and their documentation is very well written and understandable.
UPDATE: For additional information about some sites who use cakephp you can see a sample list here: http://book.cakephp.org/view/510/Sites-in-the-wild
A few notable (high traffic sites) would be:
https://addons.mozilla.org
http://scratch.mit.edu/
Kohana is my framework of choice, but I won't start to wax about its good points, I'm sure it can do anything the others can do.
Faced with the same legacy codebase problem as you described, my response was to take Kohana and disable the request routing, so that you can just use it as an include on a page-by-page basis until you're ready.
The changes are minimal; if you're interested the fork of kohana is up on github
You may need to adjust the php error level settings depending on the kludgey-ness of your codebase ;)
Zend Framework is awesome.
Even better, this excellent post from Chris Abernethy shows how to gradually migrate an existing site from a twisted plate of pasta into a nice MVC structure using ZF.
Take a look at Fat-Free Framework. It allows both procedural and OOP code, so you can have a two-stage approach. If the base code is currently procedural, then you can focus all efforts first on transformation to MVC architecture. That way you can have a proof of concept that all future efforts can be as fruitful as the first phase. Then you can move too strictly-OOP. No other framework will give you this kind of flexibility. And your end-users will not feel any delays, or worse, a culture shock.
You will get as many answers as framework users there are in this place. It's obvious that someone using framework of his choice will advice it to the others.
I opt for symfony.
However, if you care about best practices than both symfony and Zend are a good (and only) choice.
This sounds like a massive undertaking.
I can only recommend CakePHP because that is what I use, but that does not mean another framework would be less or more suitable. They're all pretty much the same when it boils down to it.
Choose on whatever criteria you wish, such as public support/user base (Cake's is massive), feel, name, colours on the webpage, whatever. But my advice is to stick with that choice and ride the learning curve.
All the frameworks mentioned are good, but you will need to rewrite loads of things like the queries. You might not want to use an ORM like Doctrine or Eloquent. Just stick with something like Active Records. Codeigniter, CakePHP and Yii will do just fine.
but it is not going to be an easy task. be warned!

PHP framework of intermediate complexity, in between CodeIgniter and Yii?

Something easy like CI (this means mandatory good, easy, up to date documentation). But also with some more features than CI.
Yii has lots of features, but it is also more complex (and it kind of forces you to have to use lots of it features). That means adding some functionality to your web-app takes three times as long because you have to figure out lots of new small functionalities of Yii.
It's kind of like the CI "gets out of your way" when it needs to, and Yii gets in your way, and if you don't do it its way, it breaks.
Features missing in CI that would be nice to have in this new "intermediate" PHP framework:
Code generation (crud).
Authentication.
Access control.
Layouts.
Widgets.
Easyer / automated pagination (like yii)
easy uri parameters
Where Yii causes me problems:
It's like for every small task there is some inbuilt functionality (this is good), but, YOU HAVE to use the inbuilt functionality, otherwise bad things happen. (CI gets out of your way, but does it too much, Yii helps a lot, but is butting in too much at times, and it forces you to sift through its documentation so that you discover these functions without which you are not able to accomplish a task that would take four time less, in CI, or in a non framework app).
Is there something in between ?
(ASP.NET MVC could be 'it', but I don't know the language, so the effort to learn it would be greater than learning Yii php framework really well, so I am looking for a PHP Framework)
I am a fan of CakePHP. I feel it has the specs you provided. If you want something more cutting edge you can take a look at Lithium
I have found some resources that kindof solve the problem, because they contain examples (Milan Babuškov's sugestion helped focus on "the solution").
Yii playground - examples
Yii cookbook - examples
Yii blog tutorial - more examples
PS. there is also google - I find solution (and examples) the fastest this way - ex: implement + pagination + yii
I've used both CI and Yii (on my own projects if that makes any difference). CI was my first introduction to MVC, and I found it easy going because it let me use any crappy structure and code. I wrote two full sites in CI (www.insolvencynews.com and www.thebigeat.com if you want to see complexity.)
I had a look at CakePHP but got NOWHERE.
Then I moved on to Yii and, like you, I found it pretty tough and rigid. But I then found that it was so powerful and extensible that I was so much more efficient. When I needed to add a few new features to the old CI sites, it was faster to rewrite the entire sites on Yii than to code up the extra features in CI.
I can't recommend a framework in the middle, but I can recommend sticking with Yii. When you say Yii gets in the way, can you give an example? Looking at DB stuff (in ascending order of dependence on Yii):
you can code using PHP's core MySQL functions.
$result = mysql_query($sql);
you can use Yii's DB abstraction layer.
Yii::app()->db->createCommand($sql)->queryAll();
You can use Yii's ActiveRecord:
Takeaway::model()->findAll();
you could try kohana (especially coming from ci)
You should check out the CI community, some of those extensions maybe have been written by someone else (I remember seeing Authentication and Components/Widgets somewhere)
Symfony is worth checking out. I personally don't like it much because they chose Prototype over jQuery for their ajax features, which is really annoying to use when you're used to jQuery.
Lithium might be good to check out too. However, it is php 5.3 only and you need to be really careful that this version of PHP is going to be supported on the server the site will be deployed on.
See this list for good comparisson:
http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks#PHP_2
From a personal point of view, I would go with symfony because of it's
rich features and
great integration with many other already bundeled components (Doctrine, Swift Mailer,..),
good (but at first complex) code generation that produces realy useable code to get you startet quickly,
powerfull use of templating (that will be the point you mention under "layouts)
many different, powerfull plug-ins, including Authentication & Access Control (it also has a plug-in to get jQuery support)
one of the best tutorials that I've seen with a framework
Downside is a
more complex structure,
IMO wired file structure,
a rather messy API documentaion compared to the tutorial
CodeIgniter is a nice framework if you don't want to create big apps but it lacks a great database integretaion and you already mentioned code generation.
im very good in Raw PHP, where the project at hand became too much to handle i decided to move to zend, with too too much complexity i finally moved to YII which really reduced the cost and overhead time for project development and most importantly for me is the simple integration of jquery, widget and advanced-OOP.
You could have a look at Qcodo / Qcubed.
They are both easy to pick up and offer code generation / ORM
Easy way to create forms in an mvc kind of way.
For what its worth, if you're looking for a PHP Framework that is like ASP.NET MVC then I think Prado is the closest thing you will find.

purpose of 3rd party mvc?

ive seen many third party mvcs or frameworks such as codeignitor , cakephp, and so on. what i want to know is what are their purposes? ive created my own framework call it an mvc or framework (in my opinion their all the same). in my framework i have all the classes in one folder called classes and all functions in another. its all organized and when a new project comes in i am able to complete it fast. i have looked at the applications that i mentioned and it seems to have huge articles and tutorials to study. what is the purpose? why not study the main language such as php, javascript/ajax or jquery, and so on then build something that you know the ins and outs of so that any project comes your way you know what to do. ive known some people who use cakephp and for every project they get stuck and need to figure out what to do. another guy i knew worked with joomla and every basic company website that came his way he would reverse engineer joomla to make it work with the site. are people using these applications because they lack knowledge in the languages? or sometimes have no choice but to make a site while lacking language and put something together.
ps: i dont want to say which is better or argue, i want to understand and see if im missing anything.
Standardized frameworks make it easy to adapt and reuse blocks of code. By using a framework such as Zend, Cake, Joomla, etc. you can find repositories of pre-made scripts and components that easily plug into your existing site.
Not only that but frameworks will (in most cases) handle a lot of complicated, repetitive tasks that are standard across most websites. Frameworks will in most cases scaffold CRUD classes against your database automatically and support a clean separation of logic and view.
Frameworks aren't for everyone and I would actually recommend that newcomers to programming or PHP learn the basic syntax and object structure. Doing so will give you a stronger understanding of how your framework behaves and make it easier for you to modify/override the existing structure. Every framework is different, and some are better suited than others for particular tasks. You should do research and testing to see which one fits your needs.
One other benefit that hasn't been mentioned yet is that using a standard framework gives some shared knowledge between developers. If you build a project with your home-grown "framework" and then someone else has to maintain it in the future, they have to totally learn your methods from scratch. However, if you had used CakePHP, and they already have experience with CakePHP, they're going to have a pretty good base to start with. That can make a huge difference in the amount of time it takes them to get comfortable with a new system.
If you want to setup a project quickly, without having to know too much about everything underneath, using one of these frameworks can be very useful. You know, to some extent at least, that they're pretty well built and reliable, and it saves you a lot of time opposed to creating everything yourself. Most of them are also easily extendable to add functionality you might need.
But you're right, it's always a good thing to learn about all the basics. That way it's a lot easier to know what these frameworks exactly do, and more importantly, why and how. And knowing that makes it easier to choose the right framework for you and extend it in a proper and efficient way.
But in the end you'll learn from both approaches, the difference being that with an existing framework you can get start working with a site (almost) right away.
I too use my own home-built framework that took many months to develop, but for every project I still weigh the pros and cons of using a 3rd party framework. The decision usually comes down to the specifics of what I want to achieve. For example, in database-centric applications, I use my own framework because the ORM solutions of most of the well-known frameworks are just downright terrible and inefficient, but for something simple like a Blog, Codeigniter might be my choice (just because I hate Wordpress, which isn't even a framework but I have to mention here how horrible it is).
But keep in mind that while you know the ins and outs of your own framework, if anyone else has to maintain your project later on it will be difficult especially if you don't document. Big frameworks like CakePHP, Kohana, Symfony, etc. are fairly well known and it's easy to find others who have experience with them. Plus if you need help, all you have to do is post on their forums and chances are you'll have a good answer.
Another point is the maintenance of a home-built framework. More often than not this comes second to developing new sites or maintaining old ones, whereas a standard framework has a team of developers behind it. Any security issues in the framework are addressed and patched more rapidly and you can usually just upgrade the framework and keep your code intact.

Categories