One of the advantages of using a framework is that some issues have been addressed and solved for us. Let's look at some helpers, how to load them, and how they make development easier.
- [Instructor] PHP frameworks were born out of the necessity of achieving similar tasks over and over. Codelgniter is not the exception. Many tasks that we as web developers do again and again have been coded for us. There are cookie operations, date formatting and conversion, forms and headers, just to name a few. These are collectively known as helpers. Let's load a few helpers and see how helpful, so to speak, they can be. Let's go ahead and open up our files.
Desktop, exercise files, chapter 1, 01 05 start. Let's go ahead and start our web server. Cd desktop, exercise files, chapter 1, 01 05 start and PHP minus s, local host 8080. Let's go ahead and open up our browser. Refresh and we have our list there. So far, we have hard coded our properties and images on our code. If we open up the view in application, views, properties, index, we can see that the image is referenced directly to the host and port.
In this case, local host 8080. But what if we needed to run the application on another port? So let's do that in config, config dot php and let's change this 8082 to 8081. And let's go ahead and stop our server with control c and restart it on port 81. If we refresh on the web browser, and change to our port 8081, we now have lost a reference to the images. This is where a very common helper comes in handy.
So let's load the url helper within our controller. We open up our controller from controllers, properties dot php and we're going to load our helper with this load helper, inside helper we'll type url. And now let's open up the view in index dot php, and instead of using this long url, we're gonna change every single one and delete it. So we're gonna select all of them and delete em.
And we're gonna copy each one and place it inside our php tag with echo base url and we're gonna paste. And we're going to do that with all the assets. Echo base url and the same. We're missing here how to close the tag with php and we're gonna do the same here. We're just gonna cut, open and close php tags, and use echo base url and inside our base url, we're just gonna paste.
And now let's refresh on our browser. And now it's loading the images correctly. We can also build links to controller and method without having to worry about the domain and port. Let's point out these buttons too. The show method. Let's go to our first link. Let's change to href and we're gonna use another helper called site url. So we open up and close php tags, echo, site, url. And inside our method, we'll just call properties slash show slash 1.
Let's refresh the page on the browser and for now if we see the link it's pointing correctly to properties show 1. Let's go back. And now let's go back and change it to the original port. So we open up config again and we change it to port 8080. We stop the server again. We restart on port 8080, go to the browser, change it to port 8080 and the url helper is working correctly since the images are showing correctly.
However, it wouldn't be very practical code wise to load over and over the url helper. Fortunately, Codelgniter comes with a mechanism to automatically load certain helpers. We do that through the auto load mechanism. So let's open up config auto load. Config, auto load, and let's go ahead and auto load our helper. In this case, helper. And we'll load url. We don't have to load it every time we need it. So let's go to our properties. We just erase this line and if we refresh it should still work.
Let's just for argument's sake, comment it on auto load, to see it won't work. It's making an error. It doesn't find a base url function. This is because we're not loading the helper yet. So let's load it again and now it's working correctly. This is a very powerful tool, auto loading. But beware, while it is very helpful, we should not abuse it. Keep in mind that every single time we open up a method in the application, even if it doesn't need said helper, we are going to load it. Functions available with a url helper, base url and site url, defer in how they build a prefix.
Base url does not include the entry point index dot php. It can be useful for loading assets, which are contained outside the bounds of the application. In this case, the images. Site url can be useful when trying to create links and refer to a controller and a method. In such case, it will include index dot php in the prefix. We've seen how helpful these helpers can be. The url helper is probably the most useful one. I recommend you read about other Codelgniter helpers before trying to do tasks on your own.
- Loading views and data from containers
- Routing with controllers
- Creating models
- Configuring the data
- Working with data, forms, sessions, and headers
- Logging and error logging
- Extending CodeIgniter to consume dependencies