DEMO - A3mercury/Laravel-Ionic-Demo-API GitHub Wiki

Introduction

  • Graduated from Harding 2 times.
  • 3 jobs since graduation.
    1. Photojojo
    2. Consulting
    3. Spur
  • Laravel and Ionic technologes.
  • Explain demo application.

Laravel API

What is Laravel?

Ionic Mobile App

What is Ionic?

Setup

  • All we will focus on is the src folder.

Setup

Folder structure:

  • app: where most of your PHP logic will reside; models, controllers, services, etc.
  • database: factories, migrations, and seeders.
  • public: compiled code for img, js, css, etc.
  • resources: defined js and css/scss and views.
  • routes: url app entry points.
  • tests: unit tests.
  • .env file.

Added code:

  • Added CORS.
  • Database migrations.
  • Database seeders.

Models

We are going to be updating the User model and creating 2 more models; Task and TaskItem.

  • php artisan make:model Task
  • php artisan make:model TaskItem

Next we are going to define the relationships between these models. A user has many tasks and a task has many task items.

Once the models have been created, we can run the DatabaseSeeder.

  • composer reseed

Routes

What happens when you go to a URL?

  • In a Laravel application, you are routed through the RouteServiceProvider class and the routes are mapped to the routes folder.
  • We define the routes in these files.
  • We will use the web.php file in our routes folder.

In the web.php file:

  • Start with a default / route and show how it is sent to the browser.
  • Then define the GET tasks route.

Controllers

Now that we have defined a route and sent it to a controller class, we need to create the controller: php artisan make:controller TasksController

Once we've created the controller we can create the index method that we are routing to and it will return a json object. Once that is completed, we can view the response in the browser as a json object. This is the data we will be using in the mobile app.

What's left?

We have to finish creating our routes for the remaining URLs we intend to use from the mobile application.

  • GET show task items. A new controller method in the TasksController should be created called show.
  • POST complete task item.
    • This will involve creating a new migration and adding the completed timestamp to the task_items table.
    • php artisan make:migration add_completed_to_task_items_table
    • Once the changes have been made: php artisan migrate
    • Create a new controller: php artisan make:controller TaskItemsController and add the complete method.
  • POST create task item. A new controller method in the TaskItemsController should be created called create.