Viewing Posts in CakePHP
I've been getting to grips with the newest release of CakePHP which at the time of writing is 1.3.3. The first thing I usually do on an application is get a simple Admin section underway and here it is.
Everything has been included in a single zip file but you must first go through the motions of creating the database and update the database.php file as well as change a few things in the core.php file.
Read More
Just a quick tip, I've been playing with the new 1.3.3 version of Cake recently and was trying to get the database sessions up and running but had difficultly finding the schema for the sessions table without using the command line to generate the table.
Here's the raw SQL that can be used to create the table. I'm sure it'll be useful in the future for me so I thought I'd share.
CREATE TABLE `cake_sessions` (
`id` varchar(255) NOT NULL DEFAULT '',
`data` text,
`expires` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
);
Now you just need to modify the core.php file so that the database is used for Sessions instead of the default:
Configure::write('Session.save', 'database');
In this post I'm going to describe how you can use Ajax using the jQuery javascript library to delete items without a refresh. This is quite an easy enhancement to achieve and will help with the usability of your applications. It isn't CakePHP specific but in this example I'm using the framework to delete items from the main index.
I'm a big fan of progressive enhancement and so the delete action will still work without Javascript enabled but this will improve the user experience a little bit by keeping the user on the same page and will display a smooth fade animation to get rid of the table row.
Read More
I've been developing a new application with CakePHP 1.2.5 and came across a really frustrating problem, which admittedly is entirely my own fault but took me ages to troubleshoot and find the source of the problem.
I'm using Xampp and Windows 7 and this problem was causing Apache to stop responding and completely crash. Quite strange behaviour for a simple application I've just started, so I started searching online for similar problems. A few were suggesting that Zend optimizer was the problem but I don't have it installed so I started debugging the code and seeing where the problem was.
Cutting a long and hair-pulling story short the problem was this piece of code:
$this->Session->setFlash('You have successfully logged in','flash_good');
Where did I go wrong? Well I hadn't created the "flash_good.ctp" layout file, one missing layout and the app was causing Apache to stop responding and crash. Now CakePHP is usually extremely good in letting you know any missing files, so I'm not sure if this is a problem with my setup or that if this was overlooked. Either way when this happens again this is the first place I'm looking.
Had similar problems? Let me know in the comments!
For a recent project I've been working on the index pages required the data to be both sortable and filterable. Luckily CakePHP comes with the Pagination class which covers the directional sorting of data and I went looking online for something that will filter the data.
Read More
My previous post on this topic is the most viewed article on the website so I thought I would re-visit it and do it all again using the latest version of Cake which at the time of writing is 1.2.3.8166
I'm going to be using a similar method as last time but a lot more refined, mainly because I'm improving as a programmer and that I'm getting more familiar with CakePHP and the best practises with using it.
Edit: I've released an updated post and sample template application to deal with CakePHP 1.3.3 go here to see it
Read More
I've been quite inactive recently and had a few comments about the "flash messages" article and it's really outdated so I thought I would do a quick update. Last time I created a new class that extended the SessionHelper but that isn't necessary and the process is much simpler. I'm using CakePHP 1.2.3.8166 in this article.
Read More
CakeBattles is an online application that allows Contenders to be pitted against each and allows users to vote on who/what would win. Each Contender can be assigned to multiple Tags which are used to create battles with similar Contenders. Each Contender can also have multiple images and these are displayed at random to keep the battles fresh.
The application was written in CakePHP and was inspired from CommandShift3 which allows users to vote on website designs. I wanted to create something similar and have been developing it in my spare time between work and freelance projects with the intent to release it online.
An example of the application in action can be seen here. Whilst developing the application I wanted to look at something pretty so I've used a bunch of hot women but I've tried to make the system as generic as possible and anything could be used e.g. films, TV shows or music albums.
Read More
My apologies for the lack of articles recently, after completing the monster Dvd application I've been pretty busy with regular work and a few freelance gigs. In a project I've been working on I had to dynamically grab some data and then use it in a select box, I thought this would be a great example to go through since its quite a common process in web development.
In this article I'm going to be using jQuery as my Javascript library, I know that CakePHP has built in support for script.aculo.us but its something I've never used and jQuery will do the job just fine.
Read More
OK so finally the full CakePHP application is coming to a close and this article is just going to wrap up a few loose ends. I've noticed a few errors and layout issues that I'm going to fix and hopefully by the end of the article the app will be fully complete.
Like all applications you could probably keep going forever adding new features, tweaking code and cleaning things up but there must come a time when you have to release it and the close the door. After this I probably wont be adding more features for some time but I do have a few ideas about great features you could try and implement.
The application can be found online at cakecatalog.jamesfairhurst.co.uk with a few more DVDs added.
Read More
In this article I'm going to go through the steps of validating CakePHP data from another Model. More specifically if you are building a Blog Application this article will demonstrate a way of validating Comment data if the add comment form is on the Post view Page.
This is something which pops up frequently on the CakePHP Google Group and is something that I've always wanted to achieve but never got round to coding a solution.
Read More
I'm currently in the process of building a new CMS in CakePHP and wanted to beef up the image capabilities so that I could upload an image once and then be able to resize the image on the fly by specifying a width/height.
I found a nice plain PHP script which does the job but I wanted something that works with CakePHP so I initially went the Helper route using this I found in the bakery. Although this is great I really wanted to resize images by changing values in the url and this is what I came up with.
Read More
Wow, just looked back at my last post date and realised its been a while. Anyway here's the latest post on creating a full application in CakePHP. Last time I finished off the header and this time I'm going to finish the footer. Check out the online version of the app cos I've been busy adding Dvds and its looking good.
From the view file I'm going to use the $this->requestAction() method to get data from the Dvd Controller. The requestAction() method is used to call a Controller's action from anywhere and can be extremely useful in our view files for requesting specific chunks of data.
Read More
Welcome to the 10th article in my series about creating a full CakePHP application from start to finish, cant believe I've managed to fill this much space with a single application but its still going and finally taking some shape and its looking pretty, pretty, pretty good (Curb your Enthusiasm Reference - Great Show).
In this article I'm going to be finishing off the header and footer of the main DVD page by adding some functionality to the form and creating some re-usable elements which grabs data from the database. I'm also going to be cleaning a few things up as and when I come across them.
Read More
I've been a bit slow on the updates recently, been super busy with life and everything but thats no excuse. In my last article I went through the process of turning a Photoshop design into HTML/CSS and now its time to get CakePHP involved.
In this article I'm going to transfer the design and code over to CakePHP by using a new Layout and making use of some elements, this isn't necessarilly hard to do but the process it quite involved so I'm going to break it down.
Read More
Howdy and welcome to the 8th article I finally got my ass in gear and started to write it so here it is. Last time I went through my design process and came up with a Photoshop mockup of what the design will look like.
In this article I'm going to be coding up my design in HTML/CSS ready for integration with CakePHP. I always try to write clean and semantic code which validates so thats going be important during this article, I also try to avoid CSS hacks and seperate style sheets for the different browsers which means that testing your code at each step in a variety of browsers is a must.
Read More
Welcome to the 7th Article about creating an online application using CakePHP 1.2. In my previous post I went through the process of implementing a basic user authentication system which will require an admin user to enter a username/password to access any of the admin controller actions.
This article is going to be a bit of diversion away from coding and will focus on the design of the application. More specifically the front end of the application and how visitors will see the front page. I'm going to show you a few places I go for design inspiration and the process involved with coming up with a design for my dvd catalog application which I've coined "CakeCatalog".
Read More
In this article I'm going to create a simple user authentication system whereby users will have to login to access any admin controller action. This is quite a basic application so I'm using the Session route of identifying a logged in user instead of the ACL route.
Read More
Welcome to the 5th Article in the series about creating a full CakePHP 1.2 Application, last time I setup the DVD Controller and View files and I'm now able to start adding DVDs to the system. As a side note I've put the application online at dvd.jamesfairhurst.co.uk so you can have a poke around and see how the application is shaping up.
In this Article I'm going to be dealing with the Genres section of the application so I can start to add / edit / delete Genres from the database. The relationship between DVDs and Genres is a HasAndBelongsToMany (HABTM) which means that a single DVD can have multiple Genres and a Genre can have multiple DVDs. I'm going to be looking at how CakePHP deals with this type of association and how to manually create these associations when saving related data.
Read More
As the title aptly suggests this is article 4 in my series of creating a full online DVD Catalog Application using CakePHP 1.2. Last time I quickly setup the Types and Locations Controller and Views so that I could start to add more data to the database.
In this article I'm going to create the DVDs controller and View files so that I can start to add / edit / delete DVDs from the database (I've gone for a Tarantino theme to begin with). This is going to be a bit more advanced because I am going to be uploading images to the server and I'm also going to use the jQuery Javascript library to progressively enhance a few forms and to stripe all the tables on the fly.
Read More
Welcome to the 3rd article in a series on creating a full CakePHP 1.2 Application. If your new and interested in the project then check out part 1 and part 2 to get fully up to speed on the application in question and see what I've done so far. Just quickly, last time I went through creating the Controller and Views for the Formats table in the database and at the end I could view / add / edit and delete formats using the application.
In this article I'm going to create the Controllers and Views for the Types and Locations. This is going to be pretty simple because they include the exact same functionality as the Formats that was created previously. At the end of the article I will be able to start adding even more data to the database and have a strong understanding of the way CakePHP deals with data.
Read More
This is the second article in the series on creating a full DVD Catalog online web application with CakePHP 1.2. Last time I went through the process of beginning a new application from scratch. I created a small brief which led to quite a few requirements that the application will need to do and from those requirements I came up with a database structure that should hold up throughout the project life cycle. Finally I sent up CakePHP on my local computer using Xampp with a Virtual Host and created the Model files for the application. If you have not yet read the first article then please go back and have a quick look so you (hopefully) know what I'm talking about.
In this article I'm going to focus on the Formats table of the application and this includes creating a controller to deal with the functionality that the application will offer such as adding, editing and deleting formats. As well as the controller I'm going to create the related views for all the Format actions and at the end of the article we will be able to start adding data to the application.
Read More
This is the first article in a series that I will use to document the process of creating a fully featured web application in CakePHP. At the moment the whole series comes in at around 8 articles but I'm sure this will grow as I write up each one. My aim is to publish one article a week detailing the steps involved with setting up and coding a full working online DVD Catalog Application that will keep track of all my DVDs. I've been meaning to create such an application for a long time so I thought it would be a great project to put on my website.
In this first article I'm going to go through the steps of setting up your new CakePHP project, go through the requirements of the system, create the database structure and finally setup the Model's and associations. For this project I'm going to be using the newest 1.2 version of CakePHP, I haven't used it before so this will be a learning process for me as well.
Read More
For the past few weeks i have been working with the CodeIgniter framework for a large scale project at work, the system is a project management application that allows different levels of users to login and track projects that belong to them. My first experience of a PHP framework was the wonderful CakePHP and my exposure to that MVC framework helped me greatly when using CodeIgniter. In this article I'm going to go through a few pro's and con's of CodeIgniter coming entirely from a perspective of a CakePHP user.
Read More
Usually when building a website that has a content management system (CMS) you will need a control panel that will be accessed via a login page so that only administrators with a username and password can login and change the site. CakePHP comes equipped with "Admin Routing" that will automatically re route URLs such as http://cakephp/admin/posts/add to a controller action called admin_add this is extremely useful for admin panels.
The only drawback of using Admin Routing is that by default it can only accommodate a single admin user, recently I've been working on a project that requires multiple levels of users e.g. admins, members, and general visitors and I wanted a way of doing this in CakePHP. Luckily I found this solution in the bakery and I've slightly modified it to meet my needs.
Read More
UPDATE: I've written an updated version of the article making this one obsolete. Please click here to view the article.
For a new project I'm working on I want to display custom error messages that are enclosed in a coloured box depending on the action, for example if a post was successfully saved i want to display a message inside a green box and if it failed i want to display the message in a red box.
Usually i just use the flash messages that are built into the Session helper, these messages are displayed on the next page after the controller has redirected the user. The major limitation of using setFlash() is that only a single message can be displayed, so when this message is displayed in the view there is no way of knowing whether it is a success or failure message. In this post i'm going to go through the process of creating your own helper which will display a custom flash message.
Read More
Form Validation is an essential task in any online application, its also an incredibly dull, tedious and repetitive task but with CakePHP you can cut down the time and effort you spend validating your form data. In a standard PHP application you will normally display the form for user input, try to validate the data, if errors exist redisplay the form showing the errors whilst keeping the data that was already submitted and go through the process again until the data successfully validates.
In this article I'm going to through the basics of creating a Model, baking a sample application and explain how you can use CakePHP to automatically validate your data.
Read More
URL slugs are extremely common on the internet today, particularly on website blogs. The title of the post is used as an identifier in the URL instead of an id number, this makes it possible for a person looking at the URL to know what the post is generally about. For example this post in your address bar is seen as "/posts/view/using_url_slugs_in_a_cakephp_app" instead of "/posts/view/1" which makes the URL meaningful and human readable.
In this article I'm going to go through the benefits of using URL slugs and how to incorporate them into the CakePHP blog application that we have been building over the previous articles.
Read More
When starting out with CakePHP I found it to be a very steep learning curve, getting used
to MVC along with learning all of CakePHP's magic methods was quite a lot of infomation to assimilate. Using a framework can sometimes lead you down the path of becoming a lazy
programmer who expects everything to be done in an abstracted way and that was my thought
process when dealing with file uploads. I expected CakePHP to do everything for me and for
a time I was a little stuck on how to go about uploading and dealing with files.
Then stupidly I realised that Cake was a PHP framework and I could deal with file uploads
like any other file upload I've been working with on normal web projects. In this article I'm going
to continue our previous blog application by adding a file upload facility. Each post will now
have the option of uploading an image which will be stored on the server and the url of that file will be stored
in the database so we can easily access it when a post is viewed.
Read More
In this article I’m going to concentrate on the ‘hasAndBelongsToMany’ (HABM) model association by integrating Tags to the Blog application that we have experimenting with. As you are probably aware Tags have become a popular method of organising posts and are really just categories. This type of relationship is different from the others you have been using because a single post can have multiple Tags and a single Tag can have multiple posts. This is different from the comments example that I looked at previously where a comment can only belong to a single Post.
As usual I suggest reading the models chapter of the online CakePHP manual, taking special attention to the ‘Defining and Querying with hasAndBelongsToMany’ section near the bottom. At first I had problems with choosing an association to use but this will come with practise and experience building different applications. If you’re having trouble just drop me an email and I’ll try to help you out.
Read More
One of the most powerful features of CakePHP is the ability to handle model associations, at its very simplest an association occurs when some data in the database is related to some other data. In normal PHP applications you would normally have to write a custom SQL statement that will grab the data using a JOIN statement. Obviously this kind of custom query can be accomplished in CakePHP but it will automatically grab all the related data as long as you have specified the relationship in the Model file.
In a simple example i'm going to stick with the mandatory 'Blog Application' and add a comment system, so that people can assign comments to posts. Before starting I recommend reading the Associations section in the manual and my previous post about using the 'bake' script.
Read More
UPDATE: This article has been updated, check out the new version right away
With most database driven websites you will normally need an admin section that requires a username and password that will give you access to a control panel where you can add/edit/delete information. With CakePHP this is quite simple with the help of “admin routing”, in the blog tutorial we can access the posts via “http://cakephp/posts/” with admin routing we can separate out all admin functions by adding “http://cakephp/admin/posts/” to the URL.
In this post we are going to re-visit the blog tutorial and implement an admin section that will require a username and password. Any request to an admin specific action will result in a login page being shown if you are not logged in, we will also be using the “Bake” command line script so please go over my previous post if you don’t know how to use it.
Read More
One of the many great things about CakePHP is the ability to use the inbuilt “bake” script that will automatically create all your controller and view files including all the source code. This is simply a great tool that will get your project up and running extremely quickly and will take away the monotony of building basic add/edit/delete actions in PHP. This article will begin by setting up everything you need to get “bake” working under windows and will then bake the blog application that was introduced in the CakePHP manual.
Read More
I primarily use Adobe Dreamweaver to create and code all of my websites, mainly because of the code highlighting and the way I organise my sites in separate folders. I realise that there are loads of text editors out there but Dreamweaver is what I’m comfortable with and it does the job.
When starting out with CakePHP I quickly found out that Dreamweaver doesn’t open .THTML and .CTP files automatically and I had to do a bit of hunting on the web to get the problem solved. The solution is pretty easy so this is just a quick post to get Dreamweaver opening .THTML and .CTP files.
Read More
Over the past few months I have been getting to grips with CakePHP, a rapid development framework written in PHP that uses the Model, View, Controller (MVC) software design pattern. If you are a PHP programmer then give this framework a try because quite frankly it’s the bee’s knees.
Although it does have quite a steep learning curve once you become familiar with the concepts it will help to improve your programming skills and will also get your database-driven websites up and running extremely quickly. This post is going to be the first of many regarding CakePHP and my plan is to release posts quite regularly that build upon the Blog tutorial found in the manual.
Read More
Back to Home Page