All through this smaller-than-expected series, we'll make our own personal Instagram duplicate, called Instapics. This little application allows you to post, as, and remark on photographs, as well as follow different clients. Considering that, how about we make a plunge!
Before we start, we should initially guarantee that we have a framework that can uphold Laravel. As indicated by the documentation, Laravel requires the accompanying:
PHP 5.3.x - Laravel utilizes a ton of PHP 5.3-explicit elements, similar to terminations, late-static restricting, and namespaces.
The FileInfo library - is empowered of course in PHP 5.3, however, on Windows frameworks, you could have to add the augmentation in your PHP.ini design record.
Mcrypt library - this is utilized by Laravel for encryption and hash age, and ordinarily comes pre-introduced with PHP.
Whenever we're finished setting up the climate, we should download Laravel and every one of the libraries we'll use for Instapics. Download the accompanying documents and spot them inside a web-open envelope:
Laravel - (Currently v3.2.1)
Twitter Bootstrap - (Currently v2.0.4)
jQuery - (Currently v1.7.2)
Inside Laravel's root envelope, you'll track down a public organizer - this is where all freely open records ought to be put away. Laravel v3.2.1 has some premade organizers inside the public envelope for our resources, CSS, img, and js organizers. Place the Twitter Bootstrap and jQuery documents in their comparing envelopes.
Before we compose any code, we want to set an encryption key for Laravel to use for our application. Laravel utilizes this to encode any information we could require encryption for, like treats. Open the application/config/application.php record and track down the key setting. Inside, the default worth will be YourSecretKeyGoesHere!.
Then, open up a Terminal/Command Prompt, peruse Laravel's root catalog, and use Artisan to create a key:
PHP craftsman key: create
This ought to naturally produce an irregular 32-character encryption key for Laravel. Assuming that you did this accurately, it ought to now seem to be the accompanying:
To recover your key, simply rehash the means!
Then, we want to change the arrangement so it can acknowledge pretty URLs and work even without guiding our solicitations toward index.php. In the application/config/application.php record, search for the accompanying:
Assuming you're utilizing this, you want to guarantee that you have mod_rewrite empowered on your web server (if you're on Apache).
Finally, we'll have to set up a virtual host for Laravel. This isn't required for an improvement climate, at the same time, for creation, we mustn't permit admittance to the Laravel library and application documents. As referenced above, in the Laravel documents you ought to see an organizer, called public, which is where all freely open records ought to go
Designing a virtual host relies upon which web server you're utilizing. Here is a model for the Apache Web Server:
We ought to likewise add the area, instapics.com, to our host's records (since this space doesn't exist). In Windows, alter the document C:\Windows\System32\drivers\etc\hosts; in Linux/OSX, you normally alter/and so on/has. Add this line to the document:
127.0.0.1 instapics.com
This will illuminate our machine that the space instapics.com will set out to 127.0.0.1, which is the neighborhood PC.
In Laravel, all solicitations to the application are planned to explicit capacities or regulators by Routes. They are answerable for training the application where URLs go. For instance, assuming we needed http://instapics.com/home to deliver the home view record, we can make the accompanying course inside routes.php, tracked down inside the application envelope:
On the other hand, if we rather need to course http://instapics.com/home to a Controller, say, the home.php regulator, we could follow through with something like this:
Route::controller('home');
This would course to the home.php regulator document. Any activity strategy there will be made accessible also.
Something imperative to note here is that as a matter of course, Laravel doesn't course to the regulators as other PHP structures do. This is by the plan. Thusly, we can make basic pages without the need to make a regulator for them. For instance, to make a static Contact Us page that rundowns down contact data, we can just follow through with something like this:
Route::any('contact-us', work()
{
return View::make('home.contact-us');
})
This will course http://instapics.com/get in touch with us and render the application/sees/home/contact-us.php record. Since we have no powerful handling on this page, we can just naturally deliver the view record, saving us an opportunity to make and design a regulator to do such.
There is quite a lot more that we can do with Routes in Laravel that it can remain all alone as its instructional exercise. Stuff like:
HTTP Verbs - Laravel empowers us to take courses, in light of the HTTP action word that was utilized in the solicitation. For instance, we can have a GET solicitation to the/home course head off to some place not the same as where the POST solicitation would go.
Trump cards - this allows us to the highway a URL with special case esteem joined to it (for example /client/(: num) where (: num) is the client's ID)
Channels - these let us run some usefulness previously or after a course is executed, contingent upon the course that was called. For instance, we can make an auth channel that will be called before all courses, except the home, and about courses. In CodeIgniter, these are like Hooks, however a lot more straightforward to carry out, because the channels depend on the courses - so we can execute them for all solicitations or simply some.
For this web application, however, we just need two or three courses. Initial, a course that maps all solicitations to regulators naturally. Open up the routes.php document, and remark out the default course to the Home regulator:
Beneath this piece of code, add the accompanying line:
Route::controller(Controller::detect());
This line will plan every one of our solicitations to every one of the regulators. There is no such thing as if the regulator or activities, the framework will return a 404 reaction.
Presently, we make a course for http://instapics.com/about. We can simply make an about regulator, yet that would be a loss since we can just put that inside the Home regulator. Add the accompanying line after the course we recently made:
Route::controller(Controller::detect());
Route::get('about', 'home@about');
This course will guide all solicitations to http://instapics.com/going to the Home regulator, and the About activity.
Regulators in Laravel are tracked down inside the application/regulators organizer. Of course, the Home regulator will acknowledge demands shipped off the base of the Laravel application. For instance, going to http://instapics.com/will go to the Home->action_index() technique.
To make a regulator, essentially make a document inside the application/regulators envelope. By show, we'll need to name the record something illustrative that will likewise be the name of the regulator class. Until further notice, we should make another regulator for our login system, called "Login":
Then, open your program and visit http://instapics.com/login. From here, you ought to see the "test" troubleshooting message that we put. Since, in the routes.php document, we set it to empower courses to every one of the regulators, this ought to work with next to no extra arrangement. Whenever you've affirmed that it's working, simply eliminate the troubleshooting message. We'll get back to this regulator in a future article.
For the present, salute yourself; you've quite recently made your most memorable Laravel regulator!