Building A Profitable Blog
The blog application uses the tags table to manage multiple tags to be applied to a post. What if you wanted to create a hierarchy of tags, or a tree of data of some kind One tag could then become a parent of another tag, and a series of tags could be assigned as children to one tag. Then, once a tag is assigned to a post, by clicking the parent tag, all the associated posts for that parent tag and its child tags would be listed for the user. This method for working with hierarchical data is more effective than building several relational databases to manage the hierarchy. Fortunately, Cake comes with the Tree behavior that maintains a tree of data dynamically.
LIGAYA TURMELLE is a full-time goddess, occasional PHP programmer, and obsessive world traveler. She lives in Guam with her husband and daughter and their two Belgian Malinois. Actively involved with the PHP community as a founding principal of phpwomen. org, administrator at codewalkers.com, roving reporter for the Developer Zone on Zend.com, PHP blogger, and long-time busy body of phpc on freenode, she hopes one day to actually meet the people she talks to. Ligaya Turmelle is a Zend Certified Engineer and MySQL Core certified. When not sitting at her computer staring at the screen, Ligaya can usually be found playing golf, scuba diving, snorkeling, kayaking, hiking, or just out playing with the dogs all over Guam.
Now that you know how to read and write files, let's try creating an application that uses the functions described in the preceding section. Assume for a second that you're developing a Weblog application, and you'd like your users to be able to configure certain aspects of this application's behavior for example, how many posts appear on the index page or the e-mail address that comments are sent to. In this case, you'd probably need to build a Web-based form that allows users to input these configuration values and saves them to a file that your application can read as needed.
For example, imagine the code needed in a weblog to retrieve the 10 most popular articles for a given tag (passed as request parameter). This code should not be in an action, but in the Model. As a matter of fact, if you need to display this list in a template, the action should simply look like
The users table is already built into your blog application. Because the Auth component hashes all passwords, you'll need to make sure the password field has enough space to store the hashes. Remember that Security.salt value in the core configuration The Auth component will use this salt value to generate its hashes, so putting a more complicated string in this preference will improve the security of your passwords. With the users table ready to store usernames and passwords, you can implement the Auth component in your blog application. First let's initialize some parameters in the Posts controller.
In this chapter, you will use the Ajax helper to build a comments section for your extensive blog application. Users will be able to add comments and vote other users' comments up or down, all without waiting for the page to reload or refresh. Along the way, I'll mention the possibilities of the Ajax helper and also introduce some Ajax methods that go beyond the scope of this helper. This chapter won't dive into all the possibilities Ajax provides, simply because that could be a book all by itself, but it will explain how Ajax can work in a Cake application and open up the door to other more complex methods you can try on your own. Let's first examine how Ajax is supposed to work and then use it to improve your blog.
In this book, I stuck with Cake's built-in style sheet and HTML markup as much as possible. You will undoubtedly want to change the design to fit your needs. Most of the time, you'll work with CSS and HTML to tweak the design as you build the application. In the case of your blog application, now is a good time to go in and apply your own CSS and graphics to the design. Maybe you'll want to hire a graphic designer to do this part.
Fortunately, Cake comes with some handy console scripts that can help you generate those views, as well as perform other important development tasks. In this chapter, I'll introduce the Bake script, which will essentially run like the scaffold and analyze the database but then provide code to edit. From there you can include your custom logic and add improved actions that go beyond simple CRUD operations. First, let's change the views a little bit for your blog application.
Many news web sites, such as Slashdot and O'Reilly's Meerkat, provide RSS feeds that update whenever new stories are published. Weblogs have also embraced RSS and having an RSS feed for your blog is a standard feature. The PHP web site also publishes RSS feeds for most PHP mailing lists.
Areas of the application that don't necessarily perform any logic or that assemble various methods from several controllers, like a home page, may need a view only. For your blog application, home needs to include only a couple of posts that link to their full-story view and the categories links you built in Chapter 14 with the Tree behavior. I would normally do this by
Again, nothing should change in terms of the final output when you refresh the Posts view. But by building these options and variables into your Blog helper, you allow for more customization down the road. Now, instead of having to get into the code of the helper itself, you can edit a configuration file (tags.php) to adjust the displays. Keep this in mind when building customized helpers. You never know where these functions will end up, especially when you distribute them through the Internet or pull them into other Cake applications.
Let's add a has and belongs to many relationship to the blog application. To do this, you will need to create a new table in the database. See Listing 4-10 for the SQL syntax to create the tags table. This tags table will hold category tags to better organize your blog posts. The name field will be an alphanumeric field to be used in accessing the tag through the URL. The longname field will store the category's display title for use in links and page headings.
Bake has provided you with a fabulous start to this simple page request. Let's go from scratch and simplify the view so as to make it more readable for a site visitor. In your blog application, this view will be a simple story display like other blogs or newspaper sites. You won't need to display fields such as the ID or the author's ID, even though those may be useful for constructing links that point to other actions or controllers.
Plugins are fairly simple to install because of their design. Simply put, they are self-contained folders that can be dropped in the app plugins directory and referenced in the model, view, or controller. They should be written so that they require little configuration. Sometimes a plugin may require a database table to be set up to work correctly, or it may need you to specify some global variables or settings in the core configuration. Whatever the case, the plugin will generally come with its own installation notes that should be fairly simple to follow. I'll show how to install an Ajax chat room plugin by Pseudocoder in your blog application to show how it's done.
To create the view_post.php script, we'll reuse some of the code and add a bit in Example 17-12. The script takes a post_id as a GET parameter and displays the posting, including its body. Comments for the posting are also listed. The user who creates the posting can delete or modify it. Likewise, users can delete or modify any comment entries they've created in your blog.
This chapter explored some of the methods for building custom routes in Cake. For this exercise, construct a route that will display blog posts by their date and ID. Providing the correct route configuration is only half the problem be sure to rebuild the View action to run the lookup correctly. Also build the admin route to handle editing and adding blog posts. You have completed the exercise correctly if posts are editable by accessing http localhost blog admin posts edit id rather than with the standard path to edit and add.
Now that Textile is running in your application, you can use its syntax when writing blog posts to easily add some text-formatting touches. Since Textile works on final output, you don't have to rework anything in the other Add or Edit actions simply adhere to the syntax when entering text, and all should appear correctly. To test your implementation of Textile, try adding some content in a blog post using some of Textile's wiki commands. For example, placing underscores around a word will result in the tag being wrapped around the word, and so forth.
Currently, the Posts controller already contains the typical CRUD actions as supplied by Bake. The first screen in the application at which the user will arrive will be the Index action of this controller (Listing 7-3). Let's customize this screen to list five blog posts with their content and author information. First, you will need to take a look at the index() action and make it perform the logic needed for the Index view to display properly.
1 Blog Posts Essentially, what you have done here is re-create the scaffolding view (assuming you're still using the cake.generic.css file instead of your own). But now that the code is right in front of you, you can toy with any aspect of it and customize it to meet your own needs. Notice on line 1 of Listing 5-4 that I've changed the tag to read Blog Posts. In Listing 5-3, the Index action runs a model function that pulls all post records and assigns them to a variable to be used in the view. In line 7 of Listing 5-3, the set() function is a Cake function that assigns a value to a view variable. In this case, the variable will be named posts and will contain the results from the find() function in the Post model. Blog Posts
When associating tables, you need to tell Cake what type of relationship each table has with the others. This blog will have a belongs to relationship in a couple of tables. First, since each blog post will have an assigned author, each blog post belongs to one user. In other words, the posts table belongs to the users table. You have placed a user_id field in the posts table as a way to save this relationship. For each record in the posts table, one of the records from the users table will be saved by assigning one of its IDs to user_id.
Each model is keyed in the array by its name and has related records attached. Not only has Cake provided you with the contents of the post record the user requested but also with the records from associated models. Say you wanted to display the name of the assigned user of this blog post by the title of the post. You could do this by performing an echo() function of the correct array, key, and value
Vt this point, you've thoroughly explored the fundamentals of developing in Cake. With the blog application as your first advanced project, you built custom resources such as controllers, models, and views and you extended those resources with others such as components, behaviors, DataSources, and helpers. But you are probably aware that the current blog application is not production-ready (you wouldn't make it available to any real users yet).
3 My Cake Blog Application Most of this is basic HTML code that displays a title called My Cake Blog Application in the browser window but does little else. Line 4 of Listing 5-1 is a bit of Cake helper code that will automatically pull in the CSS file named styles.css (you'll create this file in a second).
The classic tutorial for frameworks has been to build a blog. On Cake's official web site, there is an entire blog application tutorial. However, you need a thorough explanation of what is happening, not just a walk-through of how to build the program. So, how this tutorial differs from others is that I will systematically explain every line of code in this application. You should be able to master the concepts, not just the steps. With such mastery, you should be able to wrap your head around a lot of the features Cake has to offer and understand how to incorporate them into your own customized applications.
Class it should contain the original settings you used when first setting up the blog application (see Listing 14-1). Lines 2-13 of Listing 14-1 contain the default variable that tells Cake where to connect to the default datasource. The blog application currently connects to MySQL through the built-in MySQL DataSource. This is set in line 3 with the driver parameter. By changing this to another driver, Cake will use the new corresponding DataSource instead of MySQL. Table 14-2 shows a list of available driver options that you can use depending on the DataSource you want to connect with.
The second resources that extend the model are behaviors. These are classes that allow the data used by the model to behave differently. In short, some data-processing methods go beyond table associations and simple callbacks they may perform multiple tasks with the data as the data is saved, for instance. Behaviors are invoked when several updates need to be performed simultaneously across the database. Later, I'll describe how to use some of Cake's built-in behaviors to apply a tree structure to the blog application's tags.
Until now, I've pointed you to areas of the application by specifying a direct URL. For instance, when telling you to edit a certain action, I would write the path to that action with the full URL (like http localhost blog posts view 1). You will need to create a home page that contains all these URLs or at least a page that lists how to navigate to all the features you've built into the blog. Many novices begin with the home page before building the features of the application, which can lead to a cluttered starting point for their visitors. Design considerations aside, Cake, by its architecture and convention, promotes using dynamic methods as much as possible to generate site content. You can take advantage of what you've already built into the blog application to create the links that will direct the user to all other areas of the site.
Editing an already saved event. Notice that the default layout in the blog application is applied here, not a layout in the Calendar plugin. Figure 13-1. Editing an already saved event. Notice that the default layout in the blog application is applied here, not a layout in the Calendar plugin.
Now that I've covered the basics of installing a vendor, let's use a third-party script in your blog application. Rather than build a wiki engine from scratch, let's choose one of the many open source text-parsing engines Textile. First, obtain a copy of Textile, which is available at http textile.thresholdstate.com. In this tutorial, I'll be using version 2.0. Once you have downloaded the file and decompressed it, you'll find the main engine file named classTextile.php. Place this file in the app vendors folder for the blog application.
weblog array(11) name As you can see, this output provides some basic information about the weblog (including the link), provides links to RSS and Atom feeds, and summarizes the number of inbound and outbound links. The blogPostTags() method returns the tags based on the most popular topics referenced on the weblog. In this case, I chose Christian Stocker's blog (http blog.bitflux.ch) Stocker is one of the PHP XML developers. To reduce the length of the output, only the top three tags were selected, by means of the limit option. The following is the result of the corresponding call to var_dump() on the resulting array Based on these results, you can clearly see that the most referenced items on his weblog pertain to PHP, trips, and Switzerland, in that order.
The 1-n table relations are taken care of by the admin generator. In the example mentionned above, the weblog_comment table is related to the weblog_article table via the article_id field. If you initiate the module of the Comment class with the admin generator The comment edit action will automatically display the article_id as a select list showing the ids of the available records of the weblog_article table. In addition, if you define a_toString() method
It's an improvement on the simple guestbook and forums that started appearing on web sites years ago. They're now advanced enough to create mini-communities of people with similar interests or simply a place to post your rants about daily living. Blogs have been in the media as well. As Jeff Jarvis said in BuzzMachine (http www.buzzmachine.com), just as the raw voice of blogs makes newspeople uncomfortable. It's the sound of the future. Some blog examples are As you can see from these two blog examples, one is political, and the other is about Mark Watson's life. Of course, we've been given permission to use these blogs as examples, but go ahead and type in blogs in Google, and almost 3.5 million hits display. Weblogs are a huge trend there are sites such as http www.blogexplosion.com where you can register your blog and drive more traffic to it, or http www. blogarama.com , which is a blog search engine. The market is hot for these online diaries, or...
Wordpress (www.wordpress.org ) is a well-known Weblog (aka blog) publishing tool. It allows users to maintain and publish online diaries of their activities and supports themes, tags, automatic spell-checking, video and photo upload, and built-in spam protection. It's extremely fast, easy to set up, and simple to use three reasons that have made it popular with millions of bloggers around the world.
The MVC structure can be tricky for some, but you should feel comfortable with Cake's use of this kind of architecture. You explored adding your own customized functions to models and controllers to extend the application and explored the logic that comes from baked actions and views. In the following chapters, you'll dive in and make something of your blog application. Along the way, you'll pull in other useful built-in helpers, and in Part 3, you'll use other customizable areas that will improve the power of your application. If you think you have the hang of working in models, views, and controllers, then congratulations you've overcome the hardest part of learning Cake.
For your blog, each user will have many posts. Even if a user enters only one post, you would still want the relationship to be capable of saving more than one post per user. By telling the User model that multiple post records are associated with it, and by completing the relationship in the Post model with the belongsTo attribute, Cake can now link the two together.
Fortunately for Cake developers, Zend is less of an MVC framework than a set of libraries (or as they are often referred by Zend developers, components). Each of its components can be brought into a stand-alone PHP application through includes. As already noted, using these components as vendors allows you to integrate Zend with your Cake application. Each component will have its own specific installation procedures for which you will need to consult Zend's support web site and documentation. But the general steps when installing a Zend component in Cake are explained in the following sections. First, let's see whether you can use anything that Zend has to offer in your blog application.
We use home www htmlkb smarty as our path to the template engine files, but your path will be different based on where you installed Smarty. Note that all the template files go into the directory that smart- template_dir points to. We also set the name of the blog to Coffee Talk Blog. We're going to discuss templates for pages, which is similar to having a CSS stylesheet, yet different. Templates, like a CSS stylesheet, will enable your blog to have a consistent look and feel.
The previous line shows a new method for connecting with the Posts controller in your blog application. By using the asterisk in the path, you're essentially telling the router to accept and pass along anything it finds following the word articles. As in the base path example, the path will be tested like an if-then statement if the supplied URL begins with the word articles, then pass along whatever else follows in the URL to the Posts controller. By default, the Index action will be called. You've more or less constructed an alias for the Posts controller. Now, if you wanted, you could enter all your links that point to the Posts controller as if they pointed to an Articles controller.
A good web application that illustrates Cake's rapid development functionality and table associations is a blog. Here, you will build a simple blog application using the scaffolding feature to test associations. Later, you'll expand the blog to take on more powerful features. Mastering table associations and how they work in Cake is essential for pulling in advanced features that matter for complex web sites. A simple blog application will help us discuss table associations in an easier way and should help you if you're unfamiliar with table associations to be better equipped to use Cake. The users table will contain a running list of available authors of blog posts. When a post is created, it will be assigned an author from the users table. Notice that there is a field in the posts table named user_id. This will match up with the id field in the users table, thus linking an author to the post. Also, in the comments table, each comment will be assigned to a post in the same manner. In...
Duplication throughout the community is kept to a minimum, so expect to be directed to a thread or blog post that someone has already used to address the same topic when asking a question or requesting support. You will also probably sense a feeling of perfectionism in the community. What I mean by that is this the Cake Software Foundation, which manages Cake's development, could have easily taken shortcuts when developing the framework. Compared to other frameworks, Cake would already be considered a version 3.0 or 4.0, but it's currently at 1.2. This is due more to really tightening up the framework rather than cooking up something fast that could perform a large plethora of methods. The result of this strategy is that Cake really is more robust, mature, and well written than it otherwise would be, and you can count on it maintaining its robustness as it takes on more fancy methods. I, by no means, am diminishing the large quantity of built-in methods that rival any other...
In Part 2, you began building a blog application, and along the way I discussed some key fundamentals how to build controllers, models, and views, as well as where to use helpers to streamline development. Using and customizing other advanced features such as helpers, behaviors, DataSources, and components allows you to take advantage not only of Cake's efficient structure but of its cutting-edge functionality as well. In this part, you will build your own features and explore advanced built-in functions.
Now that I've explained plugins and how to create one, let's build a plugin for the blog application. This Calendar plugin will be very simple the author of the blog will store different events in the database, and the user will be able to visit a page showing a month with links to each event. For this plugin to be portable, I will approach writing it from a general standpoint. In other words, I won't necessarily be thinking of the blog application when writing the plugin, though ultimately the plugin will be implemented in the blog.
Understanding the process of how the router parses extensions is key to building the right route configurations and other Cake elements. First, you need to tell the router to look out for any URLs that point to a specific extension. Let's say you want to dynamically build an RSS feed for the blog application. Then you would need to use the Router parseExtensions() function for the router to detect when the .rss extension is called in the URL
Two things can happen when Bake is properly launched it will ask you what you want to bake, or it will ask you where you want a new Cake application copied. If the latter is the case, you will need to specify the path to the blog application to get Bake to work properly with your existing project. To do so, when launching Bake, use the -app parameter to specify the application's path to its app directory. Be sure to include the trailing slash (see Listing 5-6).
|Niche Blogging Profits||www.lazyblogging.com|
Official Download Link Blog Hatter Auto-Poster Software
Welcome to the webpage where you can download the legit version. The price offer is ending soon, hurry up and start enjoying your product.