Let me begin by saying this; I know that similar questions exist, but they are a couple years old and ZF is changing quickly. I want a more recent example.
It has been noted a lot that the documentation on Zend Framework is lacking. I've read a lot of it and used it to start up a few applications, but I find that it is really difficult to understand how to do things properly. My biggest issue is where various components belong, where they should be instantiated, etc.
I am trying to follow MVC best practices, with thin controllers, fat models. I am also trying to use the latest MVC stack including Zend_Application and the recommended Bootstrap classes.
I want to see an example site that uses best practices for the framework which can demonstrate the following components (ideally):
Zend_ACL/Zend_Auth: how to actually use them in an application, how to fit it in to your model, etc.
Zend_Layout: how to do this properly with common parts such as a menu
Zend_Navigation: I mostly don't know the best practice for instantiating this.
Zend_Cache: Again, where does it belong, do you use it in Controllers (probably?)
Zend_Db: I want the example to use a database obviously, but I'd prefer just using ZF and not Doctrine
Zend_Feed: I'm not completely sure how to make an RSS feed properly (i.e. set the content type, etc.)
Zend_Form: I haven't used this yet, but I understand it is good for validating data passed into the Model. I'd like to see this in action
Zend_Paginator: Again, where do you instantiate this, Model or Controller?
I know I do not need to use everything in the framework, but I want to try to use whatever may fit my needs and I'd like to learn more about it.
So do you know of a good example that uses the Zend MVC and some or all of these components? And one that follows a lot of best practices? Ideally, it is using at least 1.10, but anything 1.8 or later will suffice.
I'd recommend the following resources:
Zend Framework manual (seems obvious, but it's really great and sufficient in most cases),
Sample open source projects based on ZF, like Magento or the other discussed already,
Zend Framework tag on Github (e.g. PasteBin or ZFPlanet,
Source code from Books (e.g. StoreFront).
To answer the other questions:
Zend Cache has an action helper and application resource to make it really simple in usage,
Zend Feed in action may be seen in ZFPlanet,
Zend Paginator may be set up in bootstrap, using static methods.
You can check out cms systems build in ZF. One is TomatoCMS. The list of Projects & Applications using ZF is here. Zend Framework website is build in ZF too.
Related
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.
I'm planning on creating a small framework for a dynamic site. When creating a framework is there a standard or code of conduct which should I conclude ?
Other than a license for use, there is no standard documentation you are required to include. As for how to build the framework, as you are writing the lowest level components of the code, you get to decide how the files are built and used. Just create some PHP files and allow people to use them.
Taking a look at how existing frameworks are structured is a good way to get started. CodeIgniter is nicely commented and simple enough to understand, so that would be a good project to read through to get started.
Decide whether you framework is loosely-coupled or full-flowed MVC framework
Loosely-coupled framework is util/library based like Zend/Symfony, where you just provide all the classes and interfaces that a developer can use but you do not force a specific request-response flow.
Full-flowed provides and usually forces the developer to use the request flow that it has established, like CakePHP, CodeIgniter or Kohana.
p/s: sorry I don't know the actual terms for the framework categories.
Decide whether your framework is general purpose or targetted to specific use
General purpose is like Zend/Symfony/CakePHP where the framework is not designed with specific use in mind but rather to create a website/application. Specific use framework is like e-commerce, CMS or blog where it is designed to create a website for a very specific purpose.
You can have a look at coding standards, as well as designpatterns (especially the MVC pattern).
Here's a good tutorial which should get you started building your own PHP framework.
A good practice is to look at other PHP frameworks like:
Zend Framework
Code Igniter
Yii
Symphony
...
See list here for more PHP frameworks.
There is no standard per se. Usually you would roll out your own framework - or any product really - to combat an existing problem that persists within all of the existing frameworks. However, if it is purely for educational purposes, I would suggest you build a framework around the problems you envisage you will have.
There is some good advice regarding PHP standards here: http://www.phptherightway.com. The site advises style recommendations known as PSR-0, PSR-1, et al.
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!
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.
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