|  | 
|  | 1 | +Building an Application with Laravel | 
|  | 2 | +==================== | 
|  | 3 | + | 
|  | 4 | +This section assumes you are created the project as described in [Working with Laravel](Working-With-Laravel.md). | 
|  | 5 | + | 
|  | 6 | +Source Control | 
|  | 7 | +--------------- | 
|  | 8 | +Before you do anything, add the project directory to git, commit and push. This provides a baseline that will track all changes from the base framework. | 
|  | 9 | + | 
|  | 10 | +1. `vagrant ssh` | 
|  | 11 | +2. `cd ~/projects/laravel-api` | 
|  | 12 | +3. `git init` | 
|  | 13 | +4. `git add .` | 
|  | 14 | +5. `git commit -m "initial commit - laravel base project"` | 
|  | 15 | +6. `git remote add origin https://github.com/YOUR_REPO/laravel-api.git` | 
|  | 16 | +7. `git push -u origin master` | 
|  | 17 | + | 
|  | 18 | +Configuration | 
|  | 19 | +------------- | 
|  | 20 | + | 
|  | 21 | +In the last section I explained how to create the base Laravel project. The first step in a new project is to set the configuration as required.  | 
|  | 22 | + | 
|  | 23 | +app/config/app.php | 
|  | 24 | +===================== | 
|  | 25 | + | 
|  | 26 | +Laravel's configuration is found in app/config/app.php. You will want to change the following: | 
|  | 27 | + | 
|  | 28 | +debug: you can set this to true to enable detailed error logging when required. This setting must ALWAYS be false in Production. | 
|  | 29 | +url: set this to the url you'll be using to access the application, e.g. http://laravel-api.phplocal.dev | 
|  | 30 | +timezone: set this to the timezone that's most useful to you. It might be local time or server time. | 
|  | 31 | + | 
|  | 32 | +app/config/database.php | 
|  | 33 | +===================== | 
|  | 34 | + | 
|  | 35 | +This file is where all the database connection information is stored. You can remove all the database entries that you won't use. In our case, we can remove everything except for mysql and redis. | 
|  | 36 | + | 
|  | 37 | +Laravel Bootstrap | 
|  | 38 | +=============== | 
|  | 39 | + | 
|  | 40 | +### Environments | 
|  | 41 | + | 
|  | 42 | +In the bootstrap/start.php file you can set up environments. These allow you to vary application settings depending on where you are running. The homestead VM already has an environment added for development. The start.php file will automatically detect when your code is NOT running on the homestead VM and will detect the environment as 'production'. | 
|  | 43 | + | 
|  | 44 | +Further Reading | 
|  | 45 | +=============== | 
|  | 46 | + | 
|  | 47 | +[Configuration](http://laravel.com/docs/configuration) describes how to work with Laravel's configuration. | 
|  | 48 | +[Laravel API](http://laravel.com/api/index.html) includes a reference for all the classes used by Laravel. | 
|  | 49 | +[Lavavel Book](http://laravelbook.com/) has some good tutorials on advanced Laravel usage. | 
|  | 50 | +[Laracasts](https://laracasts.com/) are screencasts showing a wide variety of Laravel topics. | 
|  | 51 | + | 
|  | 52 | +How Does Laravel Process a Request | 
|  | 53 | +------------------------------------ | 
|  | 54 | +Just like many other frameworks, laravel uses a single php file `index.php` to handle all requests. When index.php receives a request from the web server, the following will happen: | 
|  | 55 | + | 
|  | 56 | +1. bootstrap/start.php runs - this detects the environment | 
|  | 57 | +2. Service providers loaded - these provide services to your application | 
|  | 58 | +3. app/start/*.php files are loaded - this initialises your application | 
|  | 59 | +4. app/routes.php is loaded | 
|  | 60 | +5. Application processes request and generates a Response object | 
|  | 61 | +6. Web server sends response back to client. | 
|  | 62 | + | 
|  | 63 | +Further Reading | 
|  | 64 | +------------------ | 
|  | 65 | +This document will summarise the process for creating a Laravel application. I recommend reading the [docs here](http://laravel.com/docs/lifecycle) too. | 
|  | 66 | + | 
|  | 67 | +Let's Do Something! | 
|  | 68 | +========== | 
|  | 69 | + | 
|  | 70 | +We're now ready to start building something unique. We will build out an endpoint to handle user registration, modification and authentication. | 
|  | 71 | + | 
|  | 72 | +We'll use generators to scaffold out our models, controllers, and views. | 
|  | 73 | +This guide goes through the exact steps you need to do to implement this endpoint. As a reference I've included them in [laravel-api-sample](https://github.com/joe-niland/laravel-api-sample) | 
|  | 74 | + | 
|  | 75 | +But first we need to install a couple of packages: | 
|  | 76 | + | 
|  | 77 | +Prerequisites | 
|  | 78 | +============== | 
|  | 79 | + | 
|  | 80 | +First we need to add generator support to the project. This is done with a single line in the composer.json file. | 
|  | 81 | + | 
|  | 82 | +1. Open **dev_root**/laravel-api/composer.json | 
|  | 83 | +2. Add the following: | 
|  | 84 | + | 
|  | 85 | + ` | 
|  | 86 | + "require-dev": { | 
|  | 87 | + "way/generators": "2.*", | 
|  | 88 | + "barryvdh/laravel-debugbar": "1.*" | 
|  | 89 | + }, | 
|  | 90 | + ` | 
|  | 91 | + | 
|  | 92 | + after `"require": {}` | 
|  | 93 | + | 
|  | 94 | + The generators will make it fast to build out our project. The debugbar is useful during development. | 
|  | 95 | + | 
|  | 96 | +3. Save and close the file | 
|  | 97 | +4. From within the homestead VM, in the **dev_root**/laravel-api/ directory, run the command: `composer update --dev`. | 
|  | 98 | + | 
|  | 99 | + This will download the generators project and install it. | 
|  | 100 | + | 
|  | 101 | +5. The last step is to add the generators as a Laravel Service Provider: | 
|  | 102 | + | 
|  | 103 | + Open app/config/app.php and add the following to the 'providers' array: | 
|  | 104 | + | 
|  | 105 | + ` | 
|  | 106 | + 'Way\Generators\GeneratorsServiceProvider', | 
|  | 107 | + 'Barryvdh\Debugbar\ServiceProvider', | 
|  | 108 | + ` | 
|  | 109 | + | 
|  | 110 | +Using the Generators | 
|  | 111 | +----------- | 
|  | 112 | + | 
|  | 113 | +Laravel ships with a command line tool called **artisan**. Artisan lets you interact with your project in powerful ways. One of these ways is to perform 'scaffolding', which creates new code files based on templates. | 
|  | 114 | + | 
|  | 115 | +To see the commands made available by the above Service Provider, type `php artisan` from **dev_root*/laravel-api/ | 
|  | 116 | + | 
|  | 117 | + | 
|  | 118 | + | 
|  | 119 | +### Further Reading | 
|  | 120 | + | 
|  | 121 | +[Laravel 4 Generator Docs](https://github.com/JeffreyWay/Laravel-4-Generators) | 
|  | 122 | + | 
0 commit comments