Creating my own PHP Framework - php

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.

Related

Example Zend Framework Project 1.8+

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.

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.

How do I make non-framework code to framework code? (PHP)

I just started using CakePHP and it's very different from just normal procedural or basic OOP PHP.
I am still learning PHP and still read "beginning PHP/mysql" books that teaches you basic PHP. Also lots of sites online provide code that isn't for a framework.
Is the only way to make non-framework code to say a framework, say cakePHP, by learning cakePHP thoroughly than rewriting the code yourself to fit the MVC model?
I think it would be beneficial to learn basic OOP PHP thoroughly before learning CakePHP. I admit Cake is a little restricting about naming conventions but you can customize this to your liking. I would try to rewrite a small PHP application in Cake to get familiar with it. Choosing a MVC framework does save a lot of development time if you take the time to learn on well. For Cakephp the book and API are two very valuable tools. I also suggest using Cakephp 1.3.
API
Book
CodeIgniter allows you to write things the way you want but really works best when you use it as a MVC platform. You would write all of the PHP code in the controller if you wanted and the application would still work, you could then decide to split out the code for the database into the model and then later decide to put all of the HTML into the view. That's if you would like to do it like that.
I would recommend doing tutorials and trying to use the MVC model as it has saved me a lot of time and is so much better than flat file coding.
CakePHP can be a little restricting as it requires you to follow particular naming conventions.
Hopefully this gives you a good start on a road to learning the ease and speed of MVC.

Categories