DEMO - A3mercury/Laravel-Ionic-Demo-API GitHub Wiki
Introduction
- Graduated from Harding 2 times.
- 3 jobs since graduation.
- Photojojo
- Consulting
- Spur
- Laravel and Ionic technologes.
- Explain demo application.
Laravel API
What is Laravel?
- https://laravel.com/
- A PHP framework.
- MVC framework.
- An ecosystem of PHP development.
Ionic Mobile App
What is Ionic?
- https://ionicframework.com/framework
- A cross-platform mobile application.
- Uses Angular2.
- Written in Typescript.
Setup
- All we will focus on is the
srcfolder.
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..envfile.
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 Taskphp 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
RouteServiceProviderclass and the routes are mapped to theroutesfolder. - We define the routes in these files.
- We will use the
web.phpfile in ourroutesfolder.
In the web.php file:
- Start with a default
/route and show how it is sent to the browser. - Then define the
GETtasks 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.
GETshow task items. A new controller method in theTasksControllershould be created calledshow.POSTcomplete task item.- This will involve creating a new migration and adding the
completedtimestamp to thetask_itemstable. 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 TaskItemsControllerand add thecompletemethod.
- This will involve creating a new migration and adding the
POSTcreate task item. A new controller method in theTaskItemsControllershould be created calledcreate.