building web applications using parse rest...

11

Upload: others

Post on 03-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo
Page 2: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

Building Web Applications Using Parse RESTAPIUsing Laravel V4.x to build a simple blog

Mhd Zaher Ghaibeh

This book is for sale at http://leanpub.com/building-web-applications-using-parse-rest-api

This version was published on 2015-01-10

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishingprocess. Lean Publishing is the act of publishing an in-progress ebook using lightweight toolsand many iterations to get reader feedback, pivot until you have the right book and buildtraction once you do.

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0Unported License

Page 3: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

Tweet This Book!Please help Mhd Zaher Ghaibeh by spreading the word about this book on Twitter!

The suggested tweet for this book is:

I just bought the book Building Web Applications Using Parse REST APIhttp://s.zah.me/1qWXTwX

The suggested hashtag for this book is #laravelandparseit_english.

Find out what other people are saying about the book by clicking on this link to search for thishashtag on Twitter:

https://twitter.com/search?q=#laravelandparseit_english

Page 4: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

Contents

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1What Is Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1What Is Parse Data? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Now What? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Page 5: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

IntroductionWhat Is Laravel

Quoting from Laravel Documentation¹:

Laravel Philosophy

Laravel is a web application framework with expressive, elegant syntax. We believedevelopment must be an enjoyable, creative experience to be truly fulfilling. Laravelattempts to take the pain out of development by easing common tasks used in themajority of web projects, such as authentication, routing, sessions, and caching.

Laravel aims to make the development process a pleasing one for the developerwithout sacrificing application functionality. Happy developers make the best code.To this end, we’ve attempted to combine the very best of what we have seen in otherweb frameworks, including frameworks implemented in other languages, such asRuby on Rails, ASP.NET MVC, and Sinatra.

Laravel is accessible, yet powerful, providing powerful tools needed for large, robustapplications. A superb inversion of control container, expressive migration system,and tightly integrated unit testing support give you the tools you need to build anyapplication with which you are tasked.

To simplify the idea, Laravel is a powerful web application framework, which will help you tocreate your next web application in an elegant powerful way.

Laravel Features

• RESTful Routing.• Powerful Template Engine (called Blade).• Proven Foundation, since it has been built on top of many Symfony2² Components.• Great ORM and Migration System, to deal with the database.• Supporting many Databases including : MySQL, SQLite, MSSQL, and Postgresql.• Composer powered, so that you can use Composer to install third party libraries whichyou can search for on Packagist³.

• Built with testing in mind.• Great Community.

¹http://laravel.com/docs²http://www.symfony.com³http://packagist.org

Page 6: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

Introduction 2

Please Note:This book is not intended to teach you Laravel, this book is going to demonstratehow you can use and interact with Parse Data⁴ from Parse.com⁵. If your looking for aresources to learn Laravel 4, you can check Dayle Rees⁶ book Code Bright⁷.

Installing Laravel 4

There are so many ways to install Laravel, the one which I really like is to go to Laravel website⁸,download the latest version and extract it on your computer, Open up terminal if you’re on aMac or Command Prompt if you’re using Windows, navigate to the directory where you haveextracted your files and then issue the command:

composer install

to install all the required libraries, including Laravel itself.

But also you can do it like this :

• Download and install composer from http://www.getcomopser.org⁹.• From the terminal issue the command :

composer create-project laravel/laravel jasmine --prefer-dist

• Configure your Apache virtual host to handle the domains. If you don’t know how to dothat, click here¹⁰ (I will use jasmine.dev here to reference to the blog app).

• Change the permission for storage directory to be 777 and ensure that you choose torecursively give all directories within it the same permissions.

What Is Parse Data?

Quoting from Parse.com¹¹ website:

Save flexible data objects to the cloud with SDKs for every platform. No serversnecessary.

So in easy words, you can use Parse Data as your database server, which you can interact withto save and retrieve your data whenever you need it.

⁴https://parse.com/products/data#howitworks⁵https://parse.com/⁶https://twitter.com/daylerees⁷https://leanpub.com/codebright⁸http://laravel.com⁹http://www.getcomopser.org¹⁰http://httpd.apache.org/docs/2.2/vhosts/examples.html¹¹http://www.parse.com/products/data

Page 7: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

Introduction 3

Why Using Parse.com

There is not just one reason to use Parse.com, there are many of them, a small introductioncan’t describe the full potential use of Parse Data¹², but i can say that your limitation is yourimagination. So I will list some of the features which you can get when using Parse products¹³.

• We can host our static image files (up to 10MB per file) on Parse Hosting¹⁴ Service, thisway you will be using Parse CDN to serve the images of your site.

• We can use Parse Analytics¹⁵ Service to track our API real-time usage, and to track ourcustom events via the dashboard.

• Offload User management, Quickly add user accounts to your app without having to codea full authentication system yourself.

• Easy Scalability and extendability by simply adding new feature for your application, forexample you can use Parse Social¹⁶ to easily integrate your application with Facebook andTwitter, which

You can take a look at Parse Customers¹⁷ page to get around and see what other developers hasbeen using Parse Products¹⁸ for.

Parse.com Data Features

• Full Stack Of SDK, which you can use to interact with your data.• Powerful Data Management, which you can use to manage, search, and update yourcontent without writing a single line of code.

Data Browser

• Advanced filtering directly from within the data browser.

¹²https://www.parse.com/products/data¹³https://www.parse.com/products/¹⁴https://www.parse.com/products/hosting¹⁵https://www.parse.com/products/analytics¹⁶https://www.parse.com/products/social¹⁷https://www.parse.com/customers/featured¹⁸https://www.parse.com/products

Page 8: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

Introduction 4

Data Filtering

• Ability to interact with other Parse.com¹⁹ products.

Signup With Parse.com

Since we are going to use Parse Data²⁰ as our database backend, we need to have an accountwith them, and create an app to have your own key which you will use for interacting withParse.com securely. To do this, you need to:

1. Go to Parse.com Signup page²¹.2. After you finish you will be redirected to your Dashboard and you will be greeting with

this nice box, which asks you to create your first app:

Create a new app box

1. Now lets create our new app and let’s call jasmine.

¹⁹http://parse.com/products/²⁰http://www.parse.com²¹https://parse.com/#signup

Page 9: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

Introduction 5

2. After you click the create button, you will have a small window, which will contain allyour keys which you will use to interact with Parse.com.

Your application API keys

as you can see you have many keys, and lets be honest, all you want to have is only :

1. Application ID.2. Client Key.3. REST API Key.4. Master Key.

and now we are ready to go, just remember to save them for you only, and not publish them onthe web, otherwise people will be able to access your data.

Page 10: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

Introduction 6

NowWhat?

Let’s see, we have installed Laravel 4, we have created an account on Parse.com, what else dowe need?

Actually we are missing just one component, which is the ‘PHP Parse.com Library. Sadly,Parse.com does not have such library, but that hasn’t stopped apotropaic²² from creating one,Considering this is the only library recommended by Parse.comwebsite https://parse.com/docs/api_-libraries²³, this is what we are going to use, and we might modify it if we find anything we needto change or enhance.

How to install parse.com-php-library

The easiest way to install the library is by downloading it to our app directory, and adding it tocomposer auto load so lets do that:

Why not clone it via git? It would be nice to do so, but what if you have modified thecode of the library? Next time the librarywas updated, youwill lose yourmodifications.

• Download the library from apotropaic²⁴ github repo.• Extract the library in app/libraries/parse directory.• Edit composer.json so its look like :

"autoload": {

"classmap": [

"app/commands",

"app/controllers",

"app/models",

"app/database/migrations",

"app/database/seeds",

"app/libraries/parse",

"app/tests/TestCase.php"

],

},

• Last thing you have to issue is the composer command :

²²https://github.com/apotropaic/parse.com-php-library²³https://parse.com/docs/api_libraries²⁴https://github.com/apotropaic/parse.com-php-library

Page 11: Building Web Applications Using Parse REST APIsamples.leanpub.com/...using-parse-rest-api-sample.pdf · Introduction 6 NowWhat? Let’ssee,wehaveinstalledLaravel4,wehavecreatedanaccountonParse.com,whatelsedo

Introduction 7

composer dump-autoload

and that’s it, we are done.

In the next chapter, we are going to create the Parse.com Classes which will be used by our blogto store our posts & comments on it .