Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction To Laravel

Introduction To Laravel

In this session we will explore the benefits of the Laravel framework, how to get started, and how to build an efficient platform using this powerful PHP framework. We will look at the code behind a simple application written in Laravel and dissect some of the interesting components. By the end of this talk you will be ready to start exploring PHP and Laravel for yourself.

Daniel Blair

April 11, 2016
Tweet

More Decks by Daniel Blair

Other Decks in Programming

Transcript

  1. Daniel Blair • CEO Bit Space Development Ltd • Founder

    - PanoPla • Founder - Geofy Inc. • Winnipeg Android • Web & Mobile Dev Group • Ramp Up Weekend Board Member • @CMDannCA • @BitSpaceDevelop • @PanoPlaApp • bitspacedevelopment.com
  2. Overview In this session we will explore the benefits of

    the Laravel framework, how to get started, and how to build an efficient platform using this powerful PHP framework. We will look at the code behind a simple application written in Laravel and dissect some of the interesting components. By the end of this talk you will be ready to start exploring PHP and Laravel for yourself.
  3. This is a pretty basic introduction to Laravel, if you

    have experience in the subject you may find it more beneficial to just jump in and start playing. Check out our Docker integration: https://github.com/atrauzzi/laravel- drydock
  4. What Is Laravel? • A modern framework built for creating

    high quality applications • The most scalable framework for building platforms • Essentially the community standard for PHP application development • A solid foundation for applications of any size Laravel has been used by my shop to create many applications for small and large businesses. It replaces the need for PaaS offerings like Google App Engine and Azure when mixed with Docker and Rancher. We get full control over our applications.
  5. Routing • Routing is extremely easy in Laravel. • A

    single file allows you to define all the custom routes you need and tell the application where to send your users and post their data. • Define types ◦ GET ◦ POST ◦ PUT ◦ DELETE • https://laravel.com/docs/5.0/routing Route::get('/', function() { return 'Hello World'; }); Route::post('foo/bar', function() { return 'Hello World'; }); Route::put('foo/bar', function() { // }); Route::delete('foo/bar', function() { // });
  6. Middleware HTTP middleware provide a convenient mechanism for filtering HTTP

    requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application. Of course, middleware can be written to perform a variety of tasks besides authentication. A CORS middleware might be responsible for adding the proper headers to all responses leaving your application. A logging middleware might log all incoming requests to your application. There are several middleware included in the Laravel framework, including middleware for maintenance, authentication, CSRF protection, and more. All of these middleware are located in the app/Http/Middleware directory. Directly from the Laravel documentation: https://laravel.com/docs/5.0/middleware
  7. Controllers Like all modern frameworks you have your models and

    controllers. This particular controller will give us each of the puppies in our system. You can load it with this route: Route::get('puppy/{id}', 'PuppyController@showProfile'); Which can be represented as: http://localhost/puppy/233 <?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; class PuppyController extends Controller { /** * Show the profile for the given puppy. * @param int $id * @return Response */ public function showPuppies($id) { return view('puppy.profile', ['puppy' => Puppy::findOrFail ($id)]); } }
  8. Migrations Migrations are the easiest way to manage the data

    in your database when starting your system. They define your database objects and the models in the system. It is easy for new users to pick up your application and work with it. In the example the down function is not shown, down is used for when the data is deleted. <?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
  9. The Views Your views are found under /resources/views. They are

    the HTML and visual components that are served by your application. The views can be organized under subdirectories and make it easy to reuse visual components and template data into your application. If you are familiar with Rails, you will enjoy the blade system in Laravel.
  10. Running the project To actually run the project locally we’re

    going to want to run a few commands. Composer is used to grab all our vendor dependencies. You will also create the database by running our migrations and the server can be run locally with a single command. composer update composer install php artisan migrate php artisan serve --port=8080
  11. The Goal Create a simple blog that can handle user

    login and post updates using a WYSIWYG editor. The repository can be found here: https://github. com/CMDann/thor
  12. The Database In production you will likely use a database

    like MySQL or PostreSQL, since we are doing local development we will use SQLite. In our .env file we will set up our connection type to be SQLite. The connection configurations are under /config/database.php
  13. Models & Controllers We’re going to create 2 models, one

    for our posts and one for our comments. You can find those under /app. The respteive controllers can be found under /app/http/controllers
  14. Views We have created several custom views under the /resources/views

    directory. This is where our data is going to be templated into the app. The sub directory for posts allows us to name the views so they make sense for posts.
  15. Laravel Help Docs https://laravel.com/docs/5.2 A collection of help articles and

    tutorials. This is the official Laravel documentation and probably your first go to place for learning about Laravel. The documents can be changed to suite your version of Laravel if for example you’ re working in an older version.
  16. Laracasts https://laracasts.com/ A podcast and collection of help articles to

    help you cover a wide range of topics. I have had success finding help with issues that have been quite obscure.
  17. Laravel Github https://github.com/laravel/laravel Of course the easiest place to get

    help with issues you are having with Laravel. Just like any open source community you can also submit changes for issues you are able to fix.
  18. Larachat http://larachat.co/ The official Laravel Slack group. You can join

    and contribute to the discussion here. A good place to bounce ideas off people and get help when you need it. Also a good place to offer help to others.
  19. What To Do Next? • Look at Laravel Drydock •

    https://github. com/atrauzzi/laravel-drydock • Drydock integrates Docker and Laravel to make it easy to develop locally • Explore the learning resources • Build a simple application for yourself • Join Larachat • Contribute on github
  20. More!? https://laravel.com/docs/5.2/elixir Elixir is the easiest way to use Gulp

    with Laravel. This is a great tool for defining pre-processors for managing your JavaScript and CSS. Elixir is also a tool for hooking into your testing frameworks. Laravel supports phpunit out of the box but Elixir allows you to use JavaScript testing frameworks like Karma.
  21. ORMs https://laravel.com/docs/5. 2/eloquent Eloquent is Laravel’s ORM. It is a

    tool that allows you to not write any SQL in your application. I don’ t know about you guys but I personally don’t want to write any.
  22. Fin

  23. Questions? Contact Me: • @CMDannCA • http://dan-blair.ca • [email protected]

    http://bitspacedevelopment.com • @BitSpaceDevelop Learn More: • https://github.com/CMDann • https://github.com/CMDann/thor •