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.
Related
I have been writing a lot of code for work in PHP/MySQL. So far it has all been procedural making use of functions for functionality occuring multiple times/places. Starting to find some of the site hard to manage - time to go OO.
I want to learn about MVC with object oriented PHP & MySQL. I have some experience in Java and MVC but never anything to do with web technologies, i.e. HTML/CSS/JS etc. I don't really understand how the dynamically generated HTML fits in with the classes etc.
I am after some recommndations about where I can start. Ideally some sites with great examples from the ground up. I don't really want to use a framework at this point because I find that it does too much for you. Once I understand the OO approach with MVC I'll probably use a framework to managei easier.
Cheers,
Evan
You have a lot of code, and despite being hard to change, it probably works. Making an overhaul is going to set you back a while, so the best approach is incremental. Find some ONE thing that would seriously benefit from using objects, and use objects there. Refactor as you are able to run tests. You can work this in with the ordinary flow of events, and things work out OK.
Frankly, if you're serious about removing redundancy and duplication, you'll often find places where just writing a small function can make a dent. If you do this often enough, you'll find groups of functions that work on the same data. That will suggest where to look for objects.
In other words, if you listen with the right kind of ears, the code will tell you.
If you just want a good online course, maybe you should have a look at
http://www.lynda.com/home/DisplayCourse.aspx?lpk2=653&srchtrk=index%3A1%0Alinktypeid%3A2%0Aq%3APHP%20Object%20Oriented%0Apage%3A1%0As%3Arelevance%0Asa%3Atrue%0Aproducttypeid%3A2%0Acategory_facet%3APHP
This course was really useful for a friend of me. The progress he booked when he finished this tutorial was really great. It takes you through the basics of building a CMS in PHP and object oriented.
I think you should start with small pieces to get into OOP step by step. I mean that you should write simple classes for things you use often like handling forms, image uploads, site messages, session handling.
When you get used to programming classes and working on objects it will be time to jump for something bigger like refactor whole "engine" to OOP using mvc and other stuff.
Not really an answer but too big to be a comment.
Actually HTML has nothing to so with classes and JS and CSS has nothing to do with PHP, MVC and OOP at all.
Your code should produce some data which is going to be displayed one or another way. You can use some class to render this data, but it's not that important class.
While JS and CSS are totally separate files usually, called by browser directly from the server, avoiding your application code (unless being generated dynamically). Anyway you should have not too much concern in it.
Although it's still hard and non-trivial task to tie classes hierarchy with such a discrete way of execution of a typical PHP application. Once wise man have said once, "If Windows were have to restart after each user's click, it were written completely different".
welcome to SO.
I am by no means a PHP expert, here is my thought comes on top of my head:
OO programming with PHP does help, but not very likely as the rescue to removing all the redundancies or making your code "neater". The traditional way of coding in PHP can produce nice code. OO helps to encapsulate your code to hide the implementation and reuse others' code by direct composition and/or via inheritance.
I would say the real concern here is how to decouple the "static" html from the "dynamic" PHP.
For instance, there shouldn't be many :
echo "<h1>A title</h1";
echo "<p>blah blah blah...";
Even in conditional printing.
I would say separate the whole business logic on one page/script into several functions, and at least hide the nitty-gritty inside a function.
For your questions on popular OO frameworks of PHP, CakePHP is a good one, got its inspiration from Ruby on Rails.
Read MVC Tutorial in PHP to understand what MVC is then start using a MVC Framework like Zend, CakePHP, Symphony or CodeIgniter...
So, I'm very tempted to rewrite my application using a php framework, as I think it'll make it easier for folks to get involved, as well as improving the design of the app.
CakePHP looks like the best of the PHP web frameworks. Does anyone have any experiences of it? What are the caveats I should consider going from handcoded PHP to using a framework?
Not depending on the framework you'll chose, the first thing you have to know is that :
it'll take some time for you to know it
you'll do crapping things, during that time ^^
so, take into account the fact it'll take some time before you are fully operational :-)
I think those points are the most under-estimated points : using a framework takes not much time... Using it well and to the full extend of its abilities takes... Well, a couple of months, maybe... Which means, when you are at the end of your project you'll say "I should rewrite that with all the stuff I learned while re-writing it the first time" :-D
What it means is : learn what the framework can do, learn how to use it, and use it for a while on small applications, before starting rewriting your big one !
Then, there is probably no "best framework" : one framework may be very well suited for one project, and another one may be best for a second, different project.
I've never worked with CakePHP ; I really like Zend Framework. But that is a personal opinion, and O know people who really like symfony ; they are not wrong : symfony is great -- and I am not wrong either ^^
Still, we sometimes agree on some things ; like the fact that Doctrine (default ORM layer of symfony) is really great stuff, and we tend to use in both symfony and ZF-based projects...
If motivated, you can take a look at many posts on SO, about frameworks... Here are a couple of those :
What PHP framework would you choose for a new application and why?
Is Symfony a good framework to learn?
What, in your mind, is the best PHP MVC framework?
Is Symfony a better choice than Zend for a web development shop (10+) because it is a full stack framework?
Best PHP framework for an experienced PHP developer?
Good luck with those ^^
("Which framework" is a quite pationnating -- and subjective -- question ^^ )
CakePHP has it's good parts but there is no "best" framework. Here's a thread with some clues about what's good in most popular PHP frameworks.
If you never used MVC frameworks before (and Cake is MVC framework) I think you should first familiaze yourself with MVC architecture.
"CakePHP looks like the best of the PHP
web frameworks."
This is subjective. You should compare the pros/cons of other PHP frameworks that will suit your needs.
These posts may help you:
https://stackoverflow.com/questions/2648/what-php-framework-would-you-choose-for-a-new-application-and-why
Why do I need to use a popular framework?
https://stackoverflow.com/questions/249984/php-framework-decision-analysis-paralysis
FWIW, I used it for a time when I was doing some php development. I found it easy to use, and the rapid development aspect was great, and I would imagine has just gotten better in the last 3 years. There is a ton of help in the irc channel, and the documentation is good. I didn't stick around in PHP long enough to become an expert. However, I was just starting out as a programmer then, and ran into Larry Masters (the creator, aka phpnut) and he was just a good person to talk to about design principles, and an all around nice guy. Then again you don't have to be a super nice guy to write a good web framework (I'm looking at you DHH).
Most of the frameworks "bind" you to them, meaning you have to do things their way. If you want to do something they weren't designed to do, you usually have to hack it. For example, how many PHP frameworks currently support Facebook Connect?
Personally I prefer "frameworks" that you can use only the parts you want. Zend is like this, Doctrine and Propel are ORM that are designed to be used with other code. For example, the Symfony framework can use either.
Finally, I haven't found a popular PHP framework that scales well.
I have used cakephp for a couple of projects. From the moment I learned it I have never written php again without it (unless is fun code in which I want to try some new stuff, or learn other design ideas away from MVC). As mentioned, learning it will take some time. How much time it takes really depends on your background. If you have used another MVC framework for a web scripting language then you will learn it really quick; RoR developers will pick it up within hours/days. If you don't have experience with MVC frameworks then it might take you a little bit, but it will really save you time later on the road (including in that project you start with).
Until today, I still learn new things about CakePHP every time I start a new project on it, although I do dig into a lot of its source code (you definitely don't need to do this, documentation and help boards are more than enough).
I definitely recommend you looking into it. It will save you a lot of time and get your head thinking in a different way (if you are not used to the MVC).
Best of luck.
CakePHP's convention over configuration approach has a few advantages once you learn them:
it helps to keep you code organised and understandable
makes it easier for multiple developers to collaborate on the same application
makes it possible for developers to understand other developers' applications
You have two main options when rewriting a legacy application in CakePHP:
change the database schema to reflect the conventions - resulting in less code
code your models to interface with the legacy database - this book goes into all the details
Either way, once you have done the above, it's pretty much plain sailing, and a good learning experience.
This isn't a question about what framework to use. I've learned both Rails and Django, and I write all of my webapps in PHP. My question is why bother with the frameworks? It's always taken me longer to use a framework than to reuse old MySQL code and build "models" with phpMyAdmin. I also like writing everything myself, because I know what's going on. I can still reuse functions, etc. and do things how I want, and this freedom seems to be missing from most frameworks.
I'm not saying that my way is right; in fact, I'm trying to figure out where my logic fails. The hype can't be just thin air. What am I missing?
The basic idea of a framework is to allow you to work at a higher level of abstruction and write only the code you have to write to implement your specific requirements. All the other repetitive stuff is handled for you by the framework, and probably with far fewer bugs and security holes than if you did it yourself.
It may feel like it takes longer to learn a framework than to just do it yourself using basic language features and standard APIs, but it's simply not true - not if the framework is good and the app is non-trivial, and especially not once you have learned the framework (using a different one for each new project would of course be idiotic) and factor in the time it would take to find and eliminate all the bugs and correct all the design mistakes that have long since been found, eliminated and corrected in the framework by its developer community.
Almost every developer has cowboy coder instincts that tell him "Doing things yourself is much more fun than using code others have written, and I'm sure I'm good enough to get it right the first time, so it will even be faster and better!". These instincts are almost always wrong.
Frameworks allow you to concentrate on the application itself rather than worrying about the boilerplate code that you'd otherwise have to write for every application. They allow you to structure you site in a much more logical (mostly object-oriented) way, using tried and tested design patters such as model-view-controller. The code in framework is generally more mature and of a higher standard than code you would write yourself for one-off projects as framework have a large community of developers perfecting the code perfecting the code over year. This means that framework-driven sites often perform better and are much more secure.
You also mentioned you like writing things yourself - I know where you're coming from. My solution to this was to write my own framework - I get to reuse and improve my code with every project I do and I know the entire codebase inside out.
Writing it your self may make it easier for you to understand things your self but unfortunately it can make it much harder for other developers to understand what is happening. Frameworks will often be better documented and have a larger community that can support a new developer that is working on the app that you wrote.
I think a big part of it is what you focus on. Frameworks standardize the parts that you shouldn't have to keep revisiting, which helps you focus on the application as a whole. If you reuse your own code all the time you're already using your own makeshift framework.
Your comparing a framework (Rails) to a language (PHP). A framework is going to give you pre-built components so you can spend time on what makes your project unique.
You may already have a code base that helps do this for you. Check out some of the PHP frameworks since that's where you are more comfortable. Take a look at CakePHP, CodeIgnitor and/or Zend Framework.
If you are building many small apps/sites, using a framework may make your life easier.
I think a good step for you is to create your own framework with the code you've programmed so far. ;)
Try to make your code parametrizeable, in other words: create components which you can reuse in different parts of a website (for instance: styled containers), or in different websites (form generators/validators).
You can even go further and create base-classes from which you extend new classes to build your websites. (for instance: data objects with generic select/insert/update/delete methods).
I bet this gives you the best view on why frameworks are so damn handy ;)
It'll take you longer to initially use a framework for the same reasons a PHP developer would take longer to initially use Ruby - you're not familiar with it.
Once you're familiar with them, frameworks can offer the ability to skip the mundane and focus on actually writing the important parts of the app.
You should also just use a PHP based framework like Symfony or CakePHP using them should reduce your production time considerably.
One reason to use a frame work is code separation. Take symfony for example. The model is all done with propel or doctrine libraries. Very little SQL needed. You instantiate a new object and user getters and setters, to store your data, and instead of writing SQL in your page code you create functions in the objects related to the query. When you need to access the same kind of data on different pages you are asking the model for it, keeping the business logic with the model where it should be, so there's never any difference. All the work is done in the "action controller function". You get all the data you need, and then put as little php in the display, basically just echoing the variables you got in the action controller, (with the exception of some for loops and if statements for conditionals. I have found this a more efficient way to code, and on my 2nd project saw the production time cut in half.
You don't need to learn a new language python/ruby just to use a great framework, just have to fin one that works for you.
First, PHP has frameworks too, so the question as stated misses the point.
Yes, you can write your own framework, and as Kris said, there's no shame in that. However, part of the leverage of code reuse is the collective value of the efforts of many. It's not just about reusing your own code. Frameworks encapsulate the common tasks and patterns we all share and provide well tested solutions with many iterations of improvements from the community. No individual effort is going to measure up to that, no matter who you are.
If you roll your own, it will only become world class due to the collective effort of world class people, and that will only happen if your idea merits the attention. The top frameworks out there are already proven on those criteria.
DHH is a smart guy, but the Rails we have today never could have been realized by him alone. Not even close.
If you like "writing everything yourself" as you say, then choose a framework with a core philosophy that matches yours, and start making core contributions in the areas where you can see room for improvement.
Depending on the functions of your Web Application, it can be faster to develop without a framework. For example when the Webapp is just some kind of data viewer.
But as soon as you begin to implement more advanced functions, you are much more efficient with a framework.
Try do do this from scratch:
- proper Form validation
- Handling of multiple Language and Date/Time formatting
- Authentication
See a framework as free tools and stable implemented function for you to use.
Sounds to me like you have already written your own framework in php, since you do mention code reuse.
I can imagine it being easier to use your own set of wheels instead of adapting to someone else's. No shame in that.
Frameworks are there mainly to help people who are semi-new to PHP (or the specific language it is built on) to be able to build a website to an extent that it is secure and easy enough to add on extra parts to the site without having to know a lot about the specifics like security, MySQL (or other database types). In my opinion it is a fairly good way to help break coders into a language, allowing for the fact that the framework isn't too complex of course.
EDIT The reason behind me saying they are for beginners is because myself, as a beginner has used frameworks to break myself into languages a lot better.
I have a relatively simple application up and working with some basic functionality which i have built as a bit of a project. I would like to now build on that, and add some more complex features, including login.
The code has got quite complex, and it's written in plain php, so all the presentation code is mixed in with the logic. I have decided that before I go any further I'd like to re factor it to separate this out, so it's easier to maintain and add to. I've been researching MVC and think that's the way i should be going.
I had decided to give the zend framework a go, and have spent a while trying to get to grips with it, however I have found the learning curve extremely steep as I have no object oriented experience.
Is there another framework or option that anyone could recommend? I am considering having a look at cake based on reading other posts in this forum but I'd accept any guidance - my "requirments" are
easiest to learn for non OO experience
includes some login / authentication features
handles database interaction with mysql easily.
All suggestions appreciated!
As stated very eloquently here already, frameworks are good until you want to do something they're not suited for, plus they can abstract the language to the point where you're effectively learning them rather than the language you're coding in.
I would encourage you to roll your own. If you understand the principles of MVC and you have a fair-to-middling knowledge of PHP then it won't be too hard (there's already several pointers around), plus you'll come out of it far further ahead than if you'd just used someone else's.
Smarty templating engine.
http://www.smarty.net/
I've personally used Symfony. It's a very complete and well-supported framework which is relatively easy to get started with. My experience with frameworks is that for simpler projects they can be quite a burdon.
The people at my workplace swear by Cake. It does seems to be quite flexible and certainly fast to develop with once you know it. However, I would echo da5id and say that it's always safest to build your own - you know exactly how it works, you get some good PHP experience (instead of Cake experience), and you don't have to spend hours either fighting with it to get it do do what you want, or reading up on how to use it in the first place.
I am not at all convinced that "MVC" really exists as a paradigm in PHP incidentally - the V and the C are so hopelessly intertwined in most cases because the form is the view.
Build yourself a code generator that will output PHP classes based on your database tables. If you're really clever you can get it to inspect the database for relationships and even build the joins in PHP too. Then create a second set of classes that each inherit from the table-based-class that lets you customize its behaviour. Rely on these secondary classes in your business code. (It's a form of the Generation Gap pattern)
In addition to being a framework, Fat-Free also has its own templating engine that goes beyond just variable substitution. It allows you to invoke functions and class/object methods along with familiar PHP-like expressions.
Ok, have a bunch of questions that I have been thinking about the past few days. Currently I have a site that is just a bunch of PHP files with MySQL statements mixed in with PHP, HTML and CSS, basically a huge mess. I have been tasked with cleaning up the site and have made for myself, the following requirements:
The site needs to be efficient and well laid out (the source code), I would like to be able to write as little code as possible.
There has to be good separation between structure, presentation and logic.
For whatever reason, I can't use a framework and need to keep the code maintainable and "simple" as there will be future developers working with it.
There needs to be an admin section for at least a few pages.
Saying that, this is what I know about the site as it is now:
Consists of 10-12 pages, a few are completely static, most are dynamically driven via a database and there is a huge form for users to fill out (20-30 fields) that need to be validated and checked.
The hierarchy of the site is basically 5-6 main pages and then sub-pages within those.
So, knowing those things I wanted to know if anyone had any tips/suggestions as to how to go about doing this with the least amount of headaches.
Would an OO approach be best in this situation?
Since there are many static pages and the dynamic pages just need the content filled in would it be best to use some kind of basic template?
EDIT: Thanks for the answers, when I said no frameworks I basically meant anything that would require new syntax other than PHP, as whoever gets hired to work on this site after me will probably only know PHP.
Here's an article about how to organize your PHP project, from Rasmus Lerdorf, the architect who created the language:
http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html
Despite the popularity of OO frameworks for PHP, Rasmus advocates a less OO approach. He knows more than anyone about PHP intended usage, and how to take advantage of its architecture for high-performance websites.
edit: In response to the comment by #theman, I'll concede the article isn't a fine work of writing, but I think the content is important. Using PHP as it was intended to be used is better than struggling against its weaknesses to make it fit an OO mold.
I highly recommend the Smarty templating engine for all PHP projects. It gives you an easy way to separate the logic from the presentation.
Have a look at this SO question and the answer. It's a pretty good, simple MVC design with some tips on how it can be improved. If you are concerned about maintenance, then at the very least you need to seperate presentation from logic (you need a view and controller). Smarty forces that, but it is a type of framework and you'll have additional syntax to learn.
Before you jump on Rasmus' "no framework php mvc framework" bandwagon, read some of the critical comments. Any web application structure is a framework, and Rasmus' approach isn't the best I've seen.