Join Justin Yost for an in-depth discussion in this video Overview of our current CakePHP application, part of Uploading Photos with CakePHP.
- Provided as part of the course materials is the sample project that we'll use to add photo uploading to. This project is available to run using the PHP local web server, which we'll start by navigating to the directory our project is living in, and start it using Lib/Cake/Consul/cake server and assign a port number. In this case we'll use port 8080. So if we navigate to local host 8080 in our web browser, we should see the standard CakePHP homepage.
So what does this project include? Let's look at the code first. We want to look at the model folder first. It's a standard baked application with three models. User, product, and sale. Let's look at the User Model. Users have an id, a user name, and an email address, and a relationship to sales. On to the next model, Product. Products have an id, an name, a description, and again, with a relationship to sales.
Our final model is the Sale Model. The Sale Model has an id, a user id, and a product id. The Sale Model is basically a join table between products and users, so when we record sales of a product to a user. Ok, now that we understand the basic model layer of our application, let's walk through the visual side of our application. We want to navigate again to local host 8080, and go to the users directory. We have a standard index page, listing the users we already have in our data base.
Let's add a new user. Click on the New User button. Let's name our new user Bob, and an email address of firstname.lastname@example.org, and click Submit. Now we see we have our new user in our table of users. Now that we've covered users, let's look at products. And again, we have a standard index page with products, with names, and descriptions. So let's again add a new product.
Click on the New Product button. Let's add our new product as shirt. With a simple description and submit this. Ok, we now have a Bob user who is going to have a purchase of our shirt. Let's go to our sales listing. Notice we list the product name that was purchased, and the user that purchased the product. If we click the add sale button, we can add a new sale to our Bob user for the shirt product.
We submit this, and we'll see we saved a sale for the product shirt, and to our user Bob. That's the basic functionality of our current application. This is what we'll modify so we can upload and store photos for both our users and products. the next section will cover adding a new field to our users table for our photos.
Need to add images to your PHP application? If you've already developed your app with CakePHP, the popular PHP framework, you can easily extend the framework to upload, save, and view photos. Justin Yost shows how to use CakePHP to upload individual photos to a website and add support for uploading multiple photos at one time.