Real World Laravel4 Sample
Real World Laravel4 Sample
Laravel 4
Ibrahim Yusuf
This book is for sale at http://leanpub.com/real-world-laravel4
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Love as a Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Let’s Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Adding More Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Love as a Service
In previous chapter, we have learned the basics of Laravel. Let’s get our hands dirty and create
something useful.
Sometimes I realize that my words can be extremely rude without me even notice it. I know, words
can be sharper than a sword. So, let’s help me neutralize that bad karma by making this app: Love
as a Service. This app will let its users express their love to anything, and anyone they want just by
sharing links.
In the last few years, we’ve seen many people sell things as a service. Software as a Service,
Infrastructure as a Service, Metal as a Service, and so on. It seems that everything can be a service.
That’s where the idea came from. Of course, it is just a name and nothing to do with the purpose of
this app.
Let’s Begin
Open up your Terminal, and create a fresh install of Laravel.
This may take some time, depending on your internet connection speed. After the command finished,
let’s start the server, so we can access it with our web browser.
$ cd laas
$ php artisan serve
Laravel development server started on http://localhost:8000
This is the Laravel development server, as mentioned in the previous chapter. If you have finished
using the server, you can stop the server by pressing CTRL+C. Don’t do this now, though, we are just
getting started!
Open your browser, and go to http://localhost:8000.
Love as a Service 2
Route::get('love/{to}', function($to) {
return "I love you, {$to}!";
});
This is an example of dynamic routing. We take the parameter to and echo it back to client. Let’s
enhance this a little. We will use a View this time. Create a file, named app/views/love.blade.php.
Love as a Service 3
1 <div class="panel">
2 <div class="panel-body">
3 I love you, {{ $to }}!
4 </div>
5 </div>
1 Route::get('love/{to}', function($to) {
2 return View::make('love', array('to' => $to));
3 });
I see what you did there… Alright, refresh your browser to test it.
Now, let’s style it a little bit. Create a file in your public directory, name it main.css.
1 /* public/main.css */
2 body {
3 margin: 0;
4 }
5
6 .panel {
7 position: absolute;
8 top: 50%;
9 margin-top: -80px;
10 width: 100%;
11 }
12
13 .panel-body {
14 text-align: center;
15 font-family: serif;
16 font-size: 60px;
17 }
18
19 .to {
20 text-transform: capitalize;
21 }
22
23 .sender {
24 font-size: 32px;
25 margin-left: 150px;
26 text-transform: capitalize;
27 }
Love as a Service 4
We need to reference this CSS from our page. Again, open love.blade.php, and add this line to the
beginning of the file.
1 // app/views/love.blade.php
2
3 <link rel="stylesheet" type="text/css" href="/main.css"/>
4 /* ... */
This is already good for secret admirers. However, there are circumstances where you want to put
your name on it. Let’s modify our route a bit.
Love as a Service 5
Notice the ? sign on our from parameter. It marks the parameter optional. In the closure (the
function), we specify the default value of that parameter. So, when the argument is empty, the
default value will be used.
Let’s test it in our browser. Navigate to http://localhost:8000/love/juliet/romeo.
Ah, we forgot to modify our views. Let’s do it. Edit your app/views/love.blade.php.
Refresh your browser. It should display a nice message with sender name below it. Wait. What
if we don’t supply the sender name? Try deleting the /romeo part so your address bar looks like
http://localhost:8000/love/juliet. Ah, it still works without sender name, and the browser
displays “Secret Admirer” instead. You have given your users the freedom to put the sender name
or not.
Love as a Service 6
If you want to support multiple views by only a single route, we can check if a view exists using
View::exists(). This is useful when you want to create many views or templates, so you don’t
have to create separate routes each time you add new views.
Love as a Service 7
1 // app/views/bday.blade.php
2
3 <div class="panel">
4 <div class="panel-body">
5 Happy Birthday, {{ $to }}!
6 @if ($from)
7 <div class="sender"> - {{ $from }}</div>
8 @endif
9 </div>
10 </div>
Conclusion
In just minutes, we have created a simple, yet fully functional web app available for public use.
However, this is just a tip of an iceberg. We’ve only used a few routes and views in this app, while
you can do much more with Laravel. This chapter shows you how easy it is to build a functional
web app using Laravel. At the same time, it also try to show us that good words will always look
beautiful. Even without a fancy design.
Our app is now ready, but it will have to wait until we reach Deployment chapter, when we upload
and run it to a real server. After that, we can show our love just by sharing a link. And other people
can, too!
Let’s Customize
In this app, we’ve only passed strings to URL, render the view, and send it back to the
browser. Try customizing the page’s appearance. Add new features to your liking, such as
image, music, or even video!