Home
Wiley Professional CodeIgniter
Contents
1. Q Use structured folders to separate your application s components from a central core Q Use configuration files to help you maintain vital metadata such as the location of the database base URLs and the like Q Use controller callback functions that are surfaced to the GUI If the user clicks on a link that opens post view 3 then the view callback function in the post controller is called with the ID of 3 passed to the model for data retrieval Q Allow you to extend the core code and build your own libraries and helper functions The next few sections provide an extremely concise summary of the differences between Codelgniter CakePHP and Symfony The intent here is not to list every single difference among them nor to proclaim that any one MVC framework is better than the others The intent is to discuss the differences briefly thereby giving you a more realistic appraisal of where Codelgniter fits into the scheme of MVC thinking Making Life Easier for the Developer M 10 CakePHP s automatic approach allows the developer to create web applications quickly For example the automation allows a model to map easily to a database table If you don t really care how things work underneath this approach can be strangely liberating However if you really want to understand what is happening you sometimes have to dig around in the core code Symfony took a page from Ruby on Rails providing developers with a serie
2. Welcome to the MYC World This book is about CodeIgniter and the world of Model View Controller MVC web development Before venturing into the topic of CodeIgniter it s helpful to put it into some kind of context However most programmers who are new to the world of MVC web development find some of the concepts hard to grasp at first they ve been so engrained in the old way of doing things that unlearning is difficult at first So even before you can learn anything about Codelgniter or even MVC it s probably helpful to start the discussion with something you already know and understand and then move on from there Therefore that s where this book begins with a description of a prototypical PHP project most likely very similar to anyone s very first PHP project Many PHP programmers learn PHP either as their first language having only been exposed to XHTML and CSS beforehand or after learning another similar language such as Perl Most of the time a PHP programmer s first few projects involve a pretty steep learning curve but eventually the programmer finds a comfortable groove He ends up with a project consisting of the following components Q Aseries of PHP pages with intermingled PHP commands SQL queries and XHTML Q Aseries of JavaScript and CSS files that are linked from those PHP pages QO Asseries of universal includes a footer file for all pages and a header file that contain the database connection and
3. two JavaScript and CSS files and three or four includes Everything goes well during development the project goes live and the customer is happy Of course the beginner programmer is usually blissfully unaware of some of the gotchas of this approach For example since the SQL code to retrieve the home page doesn t have any exception handling in it what happens if the database is unavailable or someone accidentally erases the home page content from the DB The code is tied to mySQL at the moment What if the customer changes database servers at some point If these thoughts occur they re usually put off For now everything works and it s time to celebrate and move on to the next project Besides the programmer thinks to himself you re using includes for the important stuff like database connections global navigation and footer content Doesn t that make the code more modular Chapter 1 Welcome to the MVC World Six months later the programmer gets a call from the client and the problems begin not big problems at first but they have a tendency to snowball The client wants to add a few database fields change the way the interface looks and make the application more flexible in certain ways Out loud the programmer tells the client that everything is OK but inside a sense of low level panic is brewing Why Because the programmer barely remembers any details about the project in question six months la
4. MVC World Figure 1 4 illustrates a typical CakePHP model Example User Model saved in app models user php lt php AppModel gives you all of Cake s Model functionality class User extends App odel Its always good practice to include this variable var name User This is used for validation see Chapter Data Validation var validate array You can also define associations See section 6 3 for more information var hasMany array Image gt array className gt Image Figure 1 4 Symfony s approach allows you to use either built in methods or raw queries to access the database abstraction layer Symfony has several built in methods to create retrieve update and delete database records Sometimes though it makes more sense to use raw SQL e g you want to return a count on a number column Using raw SQL or synthetic SQL involves the following steps getting connection building a query string preparing and executing a statement and iterating on the result set returned by the executed statement Codelgniter s approach is a bit more manual but a lot more flexible There is no standard naming convention for models and controllers Developers can manually load models or autoload them in the main configuration and the models don t have to be named a certain way to match up to tables What this means is that legacy applications are extremely easy to port over to Codelgnit
5. for that supporting mobile devices displaying content in foreign languages rendering different views of the same data such as pie chart versus spreadsheet adding extensive Ajax controls and so on So the programmer takes on the project knowing full well that the relationship with the client is the most important thing that and a sense of pride that won t let him hand the project over to a completely new programmer who will probably screw it up The first thing he decides to take on is that crazy SQL query right on the home page The thing to do here is to create a functions php file in which he stores all the functions The function for that home page content notice the minimal exception handling ends up looking like this lt php function fetchHomePage sql select from pages where status live and type home limit 1 result mysql_query sql while data mysql_fetch_object result Shp title data gt title Shp css S data gt css Shp bodycopy data gt bodycopy Shp kw data gt keywords Shp desc data gt description if count Shp return Shp Chapter 1 Welcome to the MVC World else Shp title Welcome to our web site Shp css default css Shp bodycopy This is our web site Shp kw welcome Shp desc our cool site return Shp gt Now that the data fetching code is in a separate file the home pa
6. more than zero and then dump the fields from the result set into an array with row_array In other chapters you see how to loop through multiple rows in a result set using result In this particular example each field from the database gets placed into the data array that was initialized at the top of the function Finally the function returns data and then frees the memory being used by the result set It s not necessary to do this because PHP will clear out all result set objects when page execution ends but having numerous result set objects might slow down your application It s a good habit to get into Now that you understand what s going on in the model it s time to turn your attention to the controller Basically everything you ve done in the model will become reusable data fetching functions for the controller s use Creating the Controller 14 Now that a working model is in place it s time to create a controller Controllers in Codelgniter function as the brawn of your application Anything that a user can do on your site including going to destinations should be represented in your controller First here s what a standard controller would look like Notice that in the following example the controller is for the Page application you built the model for above lt php class Page extends Controller function Page parent Controller Chapter 1 Welcome to the MVC World As bef
7. same thing Theoretically on large projects you could have developers who focus only on views controllers or models thus keeping clean lines of sight on deliverables and responsibilities Stop laughing It is possible you know If you work as an in house developer you ll quickly learn to appreciate how MVC can help teams of developers create complex projects Instead of lashing together ad hoc coding projects you ll be creating systems of value to your organization and its clients If you re a freelancer or own a small technical consulting group or development shop you ll love the power that MVC and in particular CodeIgniter brings to your arsenal of tools In this book you ll learn how to combine Codelgniter and MVC with Agile development processes to quickly build flexible and powerful applications in half the time with half the hassle Before you get too deeply into all that it s time for a quick history lesson on MVC The lesson is brief and provides some context for CodeIgniter After this chapter the focus is solely on CodeIgniter and how the processes and tools associated with it can help you build better applications A Brief History of MVC Model View Controller was first described by Xerox PARC researchers working on the Smalltalk programming language in the late 1970s and early 1980s Smalltalk was an object oriented dynamically typed reflective programming language Its first use was in the realm of educational learni
8. stylesheet type text css gt lt meta name keywords value keywords gt lt meta name description value description gt lt head gt lt body gt lt hl gt title lt h1 gt lt p gt bodycopy lt p gt lt body gt lt html gt This approach certainly involves a lot less typing and the overall aesthetics can be very familiar and pleasing to anyone who s worked with a template system in the past Also some HTML specialists who are unfamiliar with PHP find this format a bit easier to work with What about bodycopy However notice that the bodycopy pseudo variable in the example has lost some of its functionality In the previous PHP incarnation of this template the bodycopy data were processed via n12br a PHP function that converts line breaks into HTML lt br gt tags One way to fix the problem is to add two lines of processing to your controller like so function index SClaltecl mecuiatachya Oi Sthis gt load gt library parser this gt load gt model Page_model TRUE data this gt Page_model gt fetchHomePage ix the body copy fixcopy nl2br S data bodycopy Sdata bodycopy fixcopy S this gt parser gt parse home data Of course you could also rewrite the model replacing row_array with row and then processing each field as it comes off the database query That way you can process every element as it is extracted from the datab
9. track of This pervasive idea has been incorporated into every single PHP framework Codelgniter included The result has been an enormous amount of time saving and effort reduction in development From the perspective of the PHP community several difficulties remain with Ruby on Rails many PHP programmers don t have the time or desire to switch to it hosting is not as ubiquitous as with PHP and the strict database design conventions make porting legacy applications harder All of these create an understandable reluctance on the part of the PHP programmer to switch After all you create and support PHP applications and you don t feel ready to take on a brand new programming language and the learning curve associated with it Even if the language is easy to learn there are still many things about it that you haven t mastered and doing so on the client s dime seems wasteful and unethical Luckily for you several PHP MVC frameworks have emerged in the past few years among them CakePHP Symfony and Codelgniter The next section provides a brief comparison of these three PHP MVC frameworks Chapter 1 Welcome to the MVC World Comparing PHP MVC Frameworks When you look at Codelgniter Symfony and CakePHP you ll notice quite a few similarities For example all three Q Allow you to create models that bind to a data source views that display content and controllers that monitor user action and allow updates of models and views
10. age_model TRUE data this gt Page_model gt fetchHomePage S this gt load gt helper typography Sthis gt parser gt parse home data 21 Chapter 1 Welcome to the MVC World Next use the auto_typography function in the view lt html gt lt head gt lt title title lt title gt lt link href css rel stylesheet type text css gt lt meta name keywords value keywords gt lt meta name description value description gt lt head gt lt body gt lt hl gt title lt h1 gt lt p gt lt php echo auto_typography bodycopy gt lt p gt lt body gt lt html gt If you put five MVC experts in the same room and asked them to tackle this particular problem you d probably get at least three different answers if not more But you get the idea CodeIgniter is fairly easy to work with very flexible and leaves you the developer with easily maintained well organized code artifacts Conclusion This chapter serves as a basic introduction to MVC and Codelgniter s place in that world You ve learned about the concepts behind MVC and why you should use it and been given a brief history lesson Specifically you ve learned how to transform a non MVC application into a working Codelgniter application with a simple model controller and view As you continue with the rest of the book remember these key facts about Model View Controller frameworks and application
11. ase 20 Chapter 1 Welcome to the MVC World lt php class Page_model extends Model function Page_model parent Model function fetchHomePage data array Soptions array status gt live type gt home q this gt db gt getwhere pages options 1 if q gt num_rows gt 0 row q gt row Sdata title Srow gt title Sdata css Srow gt css Sdata keywords row gt keywords Sdata description row gt description S data bodycopy nl2br Srow gt bodycopy return data q gt free_result gt In a lot of ways this is a more palatable solution because now the model does all the work of preparing the data for display However others might argue that there may be a need in the future to have access to the raw bodycopy field unprocessed by n12br In that case you d want to keep the processing in the controller or create a second method in the model to address this issue There is a much better way to do all this of course and it involves using a built in Codelgniter helper called Typography This helper has an auto_typography function that does all the hard work of converting line breaks to lt br gt tags and converting quotes and dashes to the appropriate entities First load the helper in the controller function function index Sdata array Sthis gt load gt library parser this gt load gt model P
12. e model received a notification from the model and then updated the view thus mediating the flow of data With Sun s Model2 approach to MVC controllers evolved again mapping user requests to actions Model2 featured reusable but stateless views fat controllers and independent models However it became quickly apparent that some of MVC s historically relevant functionality such as views that updated themselves after receiving a message from the model didn t really map to the stateless request response world of HTTP But that didn t stop the emergence of MVC frameworks and languages for the Web So far there have been Django Struts and most famously Ruby on Rails In fact if you re a PHP developer you ve probably been watching the Ruby on Rails phenomenon with a mixture of curiosity and dread It appears from a distance that tools like Ruby on Rails allow small teams of developers to create web applications in a tenth the time with less debugging and fewer headaches Ruby on Rails web sites have slick interfaces have easy to understand URLs no more query strings and are generally more compact and secure than the sites you ve been creating with your own custom PHP One overt design strategy for Ruby on Rails is the idea of convention over configuration in other words is there a way to create a set of defaults that works 80 to 90 percent of the time thereby reducing the amount of configuration you have to keep
13. eldname gt where fieldname is your database field name and more Symfony s suite of tools for unit testing scaffolding and admin generation is probably the best of all three These tools may not be used every day but they offer plenty of shortcuts for the developer CodeIgniter comes with an amazing range of libraries and helper files that cover a lot of what you will encounter in just about every project caching security file uploads link building form building text parsing regular expressions database queries FTP e mail calendaring sessions pagination image manipulation validation XML RPC dates cookies XML and more You can also extend built in libraries and helpers and create your own Revisiting the Opening Example Now that you ve got the basics of MVC down and understand how Codelgniter differs from other PHP based MVC frameworks it s time to revisit the opening example Instead of struggling with the original code imagine that you had access to CodeIgniter and you were recoding it from scratch 12 Chapter 1 Welcome to the MVC World In the following sections you see how you d go about doing that The examples provided in the next few pages don t stop long enough to explain how to install and configure Codelgniter that s Chapter 3 or talk about all the advanced features like caching routing and the extensive libraries The assumption is that all that s been done for you and that you have a p
14. er and integration with outside database systems is generally very easy Codelgniter also allows a great deal of flexibility when querying tables For example given a database table called users the following approaches will all work if you want to extract all records place raw SQL in the query method q this gt db gt query select from users or pass in a variable sql select from users q this gt db gt query sql or use the built in get method Sq this gt db gt get users Throughout the book you ll see how easy it is to create delete and update database records In fact some of you may think that the repetition of the key database queries might be too much but getting the basics down is the right foundation for your development success You can use SQL or Codelgniter s built in methods to do this 11 Chapter 1 Welcome to the MVC World Strictly speaking CodeIgniter doesn t require models at all Although this may seem a bit confusing at first after all how is itan MVC framework without the M part dropping this requirement gives you a lot of flexibility For example it could prove useful if you re developing applications without a database or XML backend or if you need to prototype something quickly using placeholder data in your controller Views CakePHP uses layouts with placeholders title_for_layout and content_for_layout for other views This automatic approac
15. ere s normally no need to modify the model if you re going to work with the template parser Why not Well it just so happens that the template parser will accept the data structures you give it The only thing that changes is how you call out and use the data structure from the parsed template Chapter 1 Welcome to the MVC World Using Third Party Templating Systems Most of the time you ll want to use PHP templates with Codelgniter as it offers the fastest way to both develop and generate views However there might be some cases in which you ll want to use a third party templating system such as SMARTY Why use a templating system Some developers swear by them as they allow a clean separation between PHP code and the pseudo variables that designers and front end developers can be trained to use without harming the rest of the application In most cases these third party templating systems work like Codelgniter s template parser They almost always use pseudo code and pseudo variables in place of real PHP code If you ve never worked with pseudo code it looks a bit like this lt title gt title lt title gt lt h1 gt header lt h1 gt You would normally pass variable structures into the template via your controller establishing values for title and header above so they can be replaced by their corresponding pseudo variables The biggest template engine out there is SMARTY available at http smarty php
16. es options 1 if q gt num_rows gt 0 data q gt row_array return data q gt free_result 13 Chapter 1 Welcome to the MVC World The fetchHomePage function is very simple but it pays huge dividends to understand what is going on Here s what s going on in the fetchHomePage function step by step 1 The first line initializes the data array You re not required to do this but doing so is good practice and is an effective way to avoid unnecessary carping by PHP if you end up returning a null set at the end of the function The second line establishes a list of options that will be passed to the getwhere method In this case the options set are any status fields marked as live and any type fields marked as home The getwhere method is built in to CodeIgniter and allows you to extract data from a table while passing in an array that serves as the where clause in the SQL statement The this gt db gt getwhere line is where the model extracts the data from the database table You need to pass in three arguments to this method a The first argument is the name of the table b The second argument is the list of options previously discussed C The third argument is how many records you want to extract in this case the limit is set to 1 After the query runs use the num_rows method to make sure you re getting back the number of rows you want in this case anything
17. es it easy to display the information in a view You ve loaded the model and invoked the method to retrieve the data now all you need to do is print out the data to the screen In extremely simple applications you could get away with simply printing out what you need right there in the controller but that isn t smart to do unless you need a quick and easy debugging method The best way is to load a view You haven t created one yet but for now use the name home Later you can create a home php file in the Views directory When you load a view the name of the file sans php is the first argument and any data you want to pass in are the second argument Here s what the entire controller looks like lt php class Page extends Controller function Page parent Controller 15 Chapter 1 Welcome to the MVC World function index this gt load gt model Page_model TRUE Sdata content this gt Page_model gt fetchHomePage Sthis gt load gt view home data 2 gt When you look at this controller notice how organized it is and how easy it is to figure out what is going on Any visitor to the site s index or home page will kick off a discrete process Invoke the model retrieve the data and display the view with data There s nothing hidden about any of it there are no other configurations to look up Simple clean tidy Creating the View The final step creating the
18. ge is a bit simplified You could even say that things are a lot better now lt php include_once db php include_once functions php Shome fetchHomePage gt lt html gt lt head gt lt title gt lt php echo Shome title gt lt title gt lt link href lt php echo Shome css gt rel stylesheet type text css gt lt meta name keywords value lt php echo Shome kw gt gt lt meta name description value lt php echo Shome desc gt gt lt head gt lt body gt lt php include_once topnav php gt lt div id wrapper gt lt h1 gt lt php echo home title gt lt h1 gt lt p gt lt php echo nl2br home bodycopy gt lt p gt lt div gt lt php include_once footer php gt lt body gt lt html gt Soon the programmer goes through all the PHP files and converts raw SQL into functions depositing them into the same functions php file In no time at all there exists a library of functions that extract pages render calendar views create RSS feeds and do heaven knows what else The programmer ponders this strange little file that s become so bloated and decides to tackle it later there are more important things on the agenda So he gets to work on the rest of the client s requests They want to add several fields to the pages database table and they want to check for both browser language and client type Mozilla Safari IE to p
19. h is pretty handy but again it requires a certain level of discipline in your file naming Of course it s easy enough to override this feature and use any kind of naming convention you want Symfony uses templates that support XHTML and PHP snippets along with a variety of helper functions such as input_tag and link_to to help you create text inputs and links respectively Symfony also supports the use of separate layout files that can be assigned to templates CodelIgniter s approach is predictably more straightforward and flexible You can almost think of view files as includes You can load them one at a time with your controller and then render the pieces into a final view or you can just use a master view with placeholders Layout is typically controlled with CSS or you can use a lightweight templating class that uses pseudo variables in much the same way that SMARTY does There are also many helper functions available to help you create forms links and other HTML elements The preferred template format is pure PHP which means that it s easy for PHP programmers to build views and then optimize them Helpers and Libraries CakePHP is pretty light on built in helpers and libraries but it makes up for it with the Bakery an online resource full of tutorials snippets add ons and third party classes However as previously mentioned it comes with a nice set of query methods such as find findA11 query indBy lt fi
20. hoots like DigiTalk Figure 1 3 File Edit View Special Z E untitled A 1 items 2 511K in disk 3 533K available Startup Smalltalk interp New Goodies Goodies Demos goodies g Smalltalk 80 sources Goodies Utilities Smalltalk changes Figure 1 2 instance goodB Direct instance System Transcript Figure 1 3 Chapter 1 Welcome to the MVC World The next step in MVC no pun intended occurred with the arrival of the NeXT operating system and its software NeXT was a company founded by Steve Jobs in the late 1980s that remained on the market until the early 1990s when it was purchased by Apple The NeXT MVC developers found a way to create ever more powerful views and ever more fine grained Controllers i e one could now track mouse movements and click events This meant evolving the notion of the thin controller to a fat controller as the controller took a more central role in the architecture What s the difference A thin controller is one that does just the basics and relies on models and views to do most of the work A fat controller is the exact opposite a software design choice that puts most if not all the processing responsibility in the controller On the desktop front languages like Cocoa on Mac OS X provided developers with an MVC framework The Cocoa MVC framework simplified the pattern by routing all messages through the controller The controller updated th
21. nd comments Q Multiple views that display individual blog posts a list of blog posts or search results Q Acontroller that captures user interaction such as clicking on an archive link and then redirects requests to models and or views Drilling down further an MVC blog application might involve the following flow of events 1 The user visits the blog home page 2 This simple event requires a controller action for the home page which makes a quick call to the model to retrieve the last 10 blog posts in reverse chronological order The model s data are then transmitted to the view for the home page The view including the data retrieved by the model is what the user sees in his or her browser The user clicks on a link to see details on a particular blog post DARU The underlying controller captures this user input clicking the link uses the model to retrieve the blog post in question from the database and then loads the appropriate view 7 The cycle begins anew when the user clicks a link to view comments or runs a search Another way of thinking about this breakdown of roles is to map it out this way Q User Input and Traffic Control Controller Q Processing Model Q Output View This three part analogy is a much simpler way of looking at things Some might argue that the approach is much too simple After all it s possible some might argue to create an MVC application without a model or to gang p
22. net Others include TinyButStrong www tinybutstrong com and SimpleTemplate http sourceforge net projects simpletpl One of the common pitfalls of using third party templating solutions is that they can provide too much complexity and overhead both from maintenance and performance standpoints with minimal return on convenience Modifying the Controller Here s what the controller would look like lt php class Page extends Controller function Page parent Controller function index data array this gt load gt library parser this gt load gt model Page_model TRUE data this gt Page_model gt fetchHomePage Sthis gt parser gt parse home data 19 Chapter 1 Welcome to the MVC World Note the two additions to the index method loading the parser library and then calling the parser passing in as arguments the name of the view and the data retrieved from the model Also note that the data from the model can be safely stored in the data array which has also helpfully been initialized In the view you ll name your pseudo variables from the keys of this array which helpfully map to the names of the database table fields For example data css can be accessed with css Modifying the View Here s the view with pseudo variables in place instead of real PHP variables lt html gt lt head gt lt title title lt title gt lt link href css rel
23. ng and it differed from mainframe data and control structures in that Smalltalk programs involved Q Windowed user interfaces Q Object oriented programming concepts Q Passing of messages between object components Q The ability to monitor and modify their own structure and behavior In the Smalltalk world the model knew nothing about the controllers or the views that worked with it The model could be observed by the view and manipulated by the controller but other than that the model simply represented the problem domain Whenever the model changed it fired off messages to the views so they could update themselves Views themselves were subclasses of an existing View class Chapter 1 Welcome to the MVC World that knew how to render what you wanted to render For example if you wanted to draw a rectangle or a circle you d subclass an existing class for the appropriate rendering Controllers in the Smalltalk world were originally conceived as being very thin providing a means of sending messages to the model This is not a book on Smalltalk but Smalltalk s impact on modern computing in general and programming in particular can t be overstated The Macintosh Windows and X Windows development efforts have borrowed heavily from the Smalltalk windowed interface In fact the early Apple GUIs were indistinguishable from Smalltalk 80 v2 as illustrated in Figure 1 2 Many of the text based user interfaces in DOS used Smalltalk offs
24. ore this controller is bare bones consisting of just an initialization function that ties this particular controller to the master class Controller Once again notice that with this simple notation Codelgniter allows you to extend the basic core classes and create something specific and powerful with very little work When you re working with a controller every function or method maps to an address in your application If you want users to view a page with the address of page foo there had better be a Page controller and a foo method inside that controller For now all that s needed is an index function to represent the site s home page function index code goes here Right now if a user were to go to your home page they d see a blank page That s because nothing particularly interesting is happening inside the index method To change that just simply load the Page_model so you can access the fet chHomePage method you created previously lt php class Page extends Controller function Page parent Controller function index this gt load gt model Page_model TRUE Sdata content this gt Page_model gt fetchHomePage T Notice that once you load the model you want you can access the methods inside that model by simply invoking the name of the model this gt Page_model gt fetchHomePage Storing the information from the database into the data array mak
25. ress view code into the controller It s not a good idea but it can be done For right now this simple analogy holds Different ways to approach problems in an MVC fashion are explored later in the book Why Bother with MVC Although at first glance the MVC approach seems like a lot of work it really isn t When developers of the hypothetical MVC blog application want to change something about the home page they ask themselves about the nature of the change Doing so allows them to zero in on the part of the application they need to work on Chapter 1 Welcome to the MVC World Q If they want to change the number of blog posts that get retrieved or even the order in which they are displayed they update the model Q If they want to change the way the home page looks they update the view Q If they want to add a new page to their application they first add a method to their controller and then build out any supporting views to display content in the browser and models to gather data from a database The beauty of the entire system lies in the fact that none of those hypothetical actions on any given part of the MVC triumvirate affects the others It s true that changing the model in some way retrieving 15 blog posts instead of 10 will change the view but the developers didn t have to dig through their view or controller to make that change then later realize they had another SQL call embedded in other files that did the
26. roperly working Codelgniter environment The goal is to show you at high speed how a Codelgniter application might look and behave First Things First The Model Depending on your work style you may be inclined to tackle the views or controller first but a good place to start is the model Why Because the model serves as the data foundation for your entire web application Many of the controller s functions will rely on the model so it s a good idea to have those tools all ready to go All CodeIgniter models have the same initial structure lt php class Page_model extends Model function Page_model parent Model gt Notice that the name of the model in this case Page_model is both the name of the class and the name of the initializing function This file is stored in the system application models folder of your project more than likely with a name like page_model php If this were the user model you d likely call your model User_model and store it in the file user_model php Later in the book you learn the ins and outs of placing and naming files But here you need to add a few functions to your model The first function is the code that retrieves the home page lt php class Page_model extends Model function Page_model parent Model function fetchHomePage Sdata array Soptions array status gt live type gt home q this gt db gt getwhere pag
27. s Models maintain and update an application s data Views display data and user interface elements Controllers handle user events that manipulate models and render or update views Ooo o In Codelgniter Q Most of your work will be done in the controller Q Views can be regular PHP files or parsed templates with pseudo variables Q Models aren t required but most of your applications will have them The next two chapters cover Agile development practices and provide a high level overview of Codelgniter s structure and installation process By the time you finish with Chapters 1 3 you ll have the necessary background for creating the projects outlined in this book 22
28. s of command line tools to help build admin panels object relational mapping schemes views and other niceties Run a few scripts and suddenly you have a surprising amount of work done for you CodeIgniter has a different approach Just about all your work will be in the controller That s where you load libraries extract data from the model and pull in views Everything s in plain sight so it s easy to keep things organized and troubleshoot problems if and when they occur The drawback of course is that beginners tend to create very general and hence very fat controllers that can become more difficult to maintain as time goes on odels CakePHP automatically loads a model that matches the current controller Yes you can turn this off but many CakePHP development efforts leave in this admittedly useful feature CakePHP also establishes all the model associations for you allowing developers to quickly pull in for example all comments associated with a blog post after only minimal association on the developer s part This approach requires strict naming conventions for models views controllers and database tables not to mention primary key and foreign key fields It can get a bit frustrating to remember to use the singular form of the database table for your model your model is person and the database table is people but it can all be overridden And yes you can use raw SQL queries if you need to Chapter 1 Welcome to the
29. session initialization code QO Ahandful of database tables that store the application s data Chapter 1 Welcome to the MVC World In fact a newbie programmer s first PHP page probably looks a lot like this one lt php include_once db php sql select from pages where status live and type home limit 1 result mysql_query sql while data mysql_fetch_object result title data gt title Scss Sdata gt css Sbodycopy data gt bodycopy Skw data gt keywords Sdesc data gt description gt lt html gt lt head gt lt title gt lt php echo title gt lt title gt lt link href lt php echo css gt rel stylesheet type text css gt lt meta name keywords value lt php echo Skw gt gt lt meta name description value lt php echo desc gt gt lt head gt lt body gt lt php include_once topnav php gt lt div id wrapper gt lt hl gt lt php echo title gt lt h1 gt lt p gt lt php echo nl2br Sbodycopy gt lt p gt lt div gt lt php include_once footer php gt lt body gt lt html gt So far so good right The pages render quickly there s CSS for layout instead of tables so it should be easy to re skin and life is good The newbie programmer figures that keeping the project small enough means averting disaster In fact this first project does stay under a dozen PHP files in size and has one or
30. soon see MVC has been around for the past 30 years but has become increasingly popular of late especially in the world of web development What s Model View Controller Model View Controller as the name implies is a design pattern that allows developers to cleanly separate their code into three categories Models maintain data Views display data and user interface elements OQ Controllers handle user events that affect models and views Figure 1 1 illustrates the relationship among the parts The important thing to remember is that MVC takes the user into account it begins with him or her It s the user who clicks a link moves the mouse or submits a form It s the controller that monitors this activity and takes the appropriate action such as manipulating the model and updating the view displayed for interacts with manipulates Figure 1 1 Chapter 1 Welcome to the MVC World Because of MVC s three part separation developers can create multiple views and controllers for any given model without forcing changes in the model design This separation allows for easily maintained portable and organized applications that are nothing like anything you ve worked with before For example imagine that most prototypical of early 21st century web applications the blog Blogs are everywhere these days and they re not that hard to break into their constituent MVC parts Q A model that keeps track of posts a
31. tent this gt Page_model gt fetchHomePage Sthis gt load gt view home data Once the view is loaded it unwraps whatever is in data itself an array and the first thing it encounters is the content array embedded inside it You see this kind of approach taken throughout the book so it becomes second nature to you Figure 1 5 illustrates the application flow 17 Chapter 1 Welcome to the MVC World A 18 displays data for loads index controller runs fetchHomePage Figure 1 5 Slightly Different Approach Templates Along with standard PHP templates CodeIgniter offers a lightweight template parser To use the parser all you have to do is load it into your controller and pass in the data from the model The view would then use pseudo variables instead of actual PHP variables thus making your views free of PHP altogether If you are familiar with SMARTY templates you will see that these pseudo variables and pseudo structures align with your expectation of how the SMARTY world works If you are new to this type of template parsing you won t have any direct experience with it but it s pretty easy to pick up For example instead of calling title as you did before you d use title Semantically the structures and naming conventions are similar enough to the PHP variables and structures that you won t have much trouble following along One final note before continuing the discussion Th
32. ter Honestly the programmer has worked on 10 other applications since that project ended amounting to many thousands of lines of code written rewritten debugged refactored and rewritten again to meet the needs of changing requirements This is not to mention that the programmer isn t exactly indifferent to learning about programming so he s been reading books on the subject and applying what he s learned to current projects The programmer knows that the approach he took with this particular project is off the mark The approach wasn t wrong in a strictly objective way there are many ways to skin a cat after all but it sacrificed long term support and flexibility in favor of ad hoc construction and initial speed Without a doubt cleaning up the previous work will mean digging through the code to untangle PHP snippets SQL queries and XHTML markup retesting everything to make sure it s working right and making sure that the application code and interface match the database It s also likely that the client will change her mind about many things along the way requiring many course corrections read rewriting code Even if the request from the client is relatively simple involving a new skin or HTML layout the programmer could find himself carefully picking through code to make sure no vital functionality is erased The requests get more complex from there and all of them are filed under Let s hope they don t ask
33. ur HTML at this point Q All the important business logic and data extraction have occurred in the controller and model Q Because the view is PHP it can be cached by Codelgniter or by another server side optimization process 16 Chapter 1 Welcome to the MVC World Notice the use of the content array whenever you access what you need You may be wondering about that Take a look at the model again if q gt num_rows gt 0 data q gt row_array When the fetchHomePage function retrieves the home page content from the pages table notice the use of row_array which converts the results into an array The keys of that array are the field names with field values populating the array values In other words the row_array method is a simple way of keeping parity between your database tables and result set objects If you were to inspect the data array within the model with print_r you d probably see something like this Array Eaei gt Al title gt test keywords gt test description gt test status gt live bodycopy gt test css gt default css Once called from the Page controller however this array is dropped inside data content which when passed to the view is accessible via the content array Why Because you told the template to accept the data array when you loaded the view function index this gt load gt model Page_model TRUE data con
34. ush custom content out to different users They also want to incorporate analytics tracking packages from third party vendors Also and they know it s last minute but one of the founders of the company just got back from a conference and is really hot on using Ajax Through it all the programmer s probably thinking There s got to be a better way Well there is and it involves learning about a way of thinking called Model View Controller MVC With MVC developers can separate their code into distinct parts making the entire application easier to develop maintain and Chapter 1 Welcome to the MVC World extend Furthermore MVC frameworks are usually pretty structured allowing the developer to concentrate on what s important to the client and the project at hand and not worry about other issues that affect every project such as security and caching The point here is not to chastise the programmer or call out deficiencies in his approach Everyone has been there and done that The point is to show you a better way inspired by a belief in the transformative power of MVC Agile methodologies and CodeIgniter Believe it All these things working together can radically change the way you work and interact with clients You ll get to CodeIgniter and Agile methodologies very soon but for now it s time to focus on MVC concepts Once you have a strong foundation in the basics the rest should progress naturally As you ll
35. view is quite possibly the easiest Simply create the appropriate file in the system application views folder in this case a file called home php lt html gt lt head gt lt title gt lt php echo content title gt lt title gt lt link href lt php echo content css gt rel stylesheet type text css gt lt meta http equiv Content Type content text html charset UTF 8 gt lt meta name keywords value lt php echo content keywords gt gt lt meta name description value lt php echo S content description gt gt lt head gt lt body gt lt hi gt lt php echo content title gt lt h1 gt lt p gt lt php echo nl2br Scontent bodycopy gt lt p gt lt body gt lt html gt Please note that the content css in this code example is just a file name You re not storing the entire CSS file in the database You could if you wanted to but it makes more sense to keep this information in a separate file Notice that the view is mostly HTML markup with PHP commands interspersed The view contains a title tag with dynamic content pulled in from the database a dynamically inserted CSS filename again from the database and other information such as the UTF 8 charset declaration You may be wondering what the point of all this is if you re eventually going to have PHP mixed in with your HTML It s important to understand that Q You have a lot less PHP in yo
Download Pdf Manuals
Related Search
Related Contents
Black & Decker AS870 Use & Care Manual ケミスタ-産業株式会社 Ariac utensili elettronici Philips RI515/15 (1)24.5KB User Manual Copyright © All rights reserved.