SlideShare a Scribd company logo
API DEVELOPMENT WITH
LARAVEL
@MICHAELPEACOCK
CONFOO, MARCH 2018
WHAT’S IN STORE?
• Walk through building an API with Laravel
• API Routes and Middleware
• API Controllers
• API Resources
• API Authentication
@MICHAELPEACOCK
• Freelance developer & consultant CTO
• Mainly helping early stage SaaS businesses or businesses looking to get into that area
• Part of the @phpne team
• Occasional technical author and speaker
SETUP
LET’S SCAFFOLD A PROJECT
CREATE A NEW LARAVEL PROJECT
composer create-project --prefer-dist laravel/laravel api-development
CREATE A MIGRATION FOR OUR INITIAL MODELS
php artisan make:migration initial_models
API Development with Laravel
API Development with Laravel
API Development with Laravel
RUN OUR DATABASE MIGRATION
php artisan migrate
CREATE A MODEL
php artisan make:model Speaker --factory
API Development with Laravel
API Development with Laravel
API Development with Laravel
API Development with Laravel
CREATE A NEW SEEDER
php artisan make:seeder SponsorSeeder
API Development with Laravel
API Development with Laravel
SEED THE DATABASE
php artisan db:seed
API CONTROLLERS
CREATE AN API CONTROLLER
php artisan make:controller Api/EventController --model=Event --api
API Development with Laravel
CREATE A NEW REQUEST CLASS
php artisan make:request CreateEventRequest
API Development with Laravel
API Development with Laravel
CREATE A NESTED API CONTROLLER
php artisan make:controller Api/EventTalkController
--model=Talk --api --parent=Event
api/events/{event}/talks…
API Development with Laravel
API ROUTES
ROUTING YOUR API REQUESTS
MIDDLEWARE: KERNEL.PHP
RATE LIMITING
throttle:60,1
DYNAMIC RATE LIMITING
API Development with Laravel
API Development with Laravel
ROUTE SERVICE PROVIDER
A MORE SUITABLE NAMESPACE
API Development with Laravel
OUR API SO FAR
VIEWING AN EVENT
VIEWING A LIST OF EVENTS
EVENT PAGINATION
HIDING CERTAIN PROPERTIES
API Development with Laravel
FORM VALIDATION
CAREFUL HOW YOU SET RELATED MODELS PRIOR TO
RETURNING
API Development with Laravel
CAREFUL HOW YOU GET RELATED MODELS
or
API Development with Laravel
API RESOURCES
TRANSFORMING YOUR MODELS AND COLLECTIONS
DO WE NEED API RESOURCES?
• No, but you should use them!
• By default, models and collections are converted to json anyway
• Why should you use them?
• Separation of model and API response
• Makes versioning easier
• Greater flexibility in terms of what is returned
CREATE A NEW RESOURCE
php artisan make:resource Event
API Development with Laravel
API Development with Laravel
API Development with Laravel
API Development with Laravel
WITHOUT DATA WRAPPING
API Development with Laravel
CUSTOMISING DATA WRAPPING
COLLECTION
OurResource::collection($collection);
CREATE NEW RESOURCES FOR COLLECTIONS
php artisan make:resource Events --collection
php artisan make:resource EventCollection
API Development with Laravel
API Development with Laravel
API Development with Laravel
NESTED COLLECTION
API Development with Laravel
WRAPPING NESTED RESOURCES
API Development with Laravel
IT WON’T DOUBLE NEST
NESTED PAGINATION
• Doesn’t seem to be supported
• I’d hoped that if an event had talks, I could paginate the talks, however the pagination meta data is
dropped from the response when it’s a nested item
CUSTOMISING THE LINKS & META DATA
API Development with Laravel
API Development with Laravel
API Development with Laravel
API Development with Laravel
CONDITIONALS: RELATIONSHIPS
CONDITIONALS: ATTRIBUTES
API Development with Laravel
API AUTHENTICATION
INSTALL AND SETUP PASSPORT
composer require laravel/passport
php artisan migrate
php artisan passport:install
HASAPITOKENS
PASSPORT ROUTES
API Development with Laravel
API Development with Laravel
API BASED
REGISTRATION
API AUTHENTICATION
Authorization: Bearer YourAccessTokenHere
API Development with Laravel
API Development with Laravel
API Development with Laravel
THAT’S A WRAP!
Ad

Recommended

Swagger
Swagger
NexThoughts Technologies
 
API Docs with OpenAPI 3.0
API Docs with OpenAPI 3.0
Fabrizio Ferri-Benedetti
 
GitHub Copilot.pptx
GitHub Copilot.pptx
Luis Beltran
 
Introduction to github slideshare
Introduction to github slideshare
Rakesh Sukumar
 
Angular Lifecycle Hooks
Angular Lifecycle Hooks
Squash Apps Pvt Ltd
 
Api presentation
Api presentation
Tiago Cardoso
 
Angular
Angular
Mouad EL Fakir
 
Swagger With REST APIs.pptx.pdf
Swagger With REST APIs.pptx.pdf
Knoldus Inc.
 
Git and Github slides.pdf
Git and Github slides.pdf
Tilton2
 
Introduction to shodan
Introduction to shodan
n|u - The Open Security Community
 
Laravel ppt
Laravel ppt
Mayank Panchal
 
Angular 8
Angular 8
Sunil OS
 
Angular
Angular
sridhiya
 
Lecture 2_ Intro to laravel.pptx
Lecture 2_ Intro to laravel.pptx
SaziaRahman
 
API Business Models
API Business Models
John Musser
 
Building blocks of Angular
Building blocks of Angular
Knoldus Inc.
 
REST API
REST API
Tofazzal Ahmed
 
Spring Security
Spring Security
Knoldus Inc.
 
Angular 9
Angular 9
Raja Vishnu
 
Introducing Swagger
Introducing Swagger
Tony Tam
 
APIsecure 2023 - Exploring Advanced API Security Techniques and Technologies,...
APIsecure 2023 - Exploring Advanced API Security Techniques and Technologies,...
apidays
 
Angularjs PPT
Angularjs PPT
Amit Baghel
 
Swagger UI
Swagger UI
Walaa Hamdy Assy
 
Best PHP Frameworks
Best PHP Frameworks
Clixlogix Technologies
 
Git and github
Git and github
Sayantika Banik
 
What is Angular?
What is Angular?
Albiorix Technology
 
Understanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple Steps
Tessa Mero
 
Dapper
Dapper
Suresh Loganatha
 
Ultimate Laravel Performance Optimization Guide
Ultimate Laravel Performance Optimization Guide
CMARIX TechnoLabs
 
Building RESTful APIs with Laravel A Complete Guide.pdf
Building RESTful APIs with Laravel A Complete Guide.pdf
Grey Space Computing
 

More Related Content

What's hot (20)

Git and Github slides.pdf
Git and Github slides.pdf
Tilton2
 
Introduction to shodan
Introduction to shodan
n|u - The Open Security Community
 
Laravel ppt
Laravel ppt
Mayank Panchal
 
Angular 8
Angular 8
Sunil OS
 
Angular
Angular
sridhiya
 
Lecture 2_ Intro to laravel.pptx
Lecture 2_ Intro to laravel.pptx
SaziaRahman
 
API Business Models
API Business Models
John Musser
 
Building blocks of Angular
Building blocks of Angular
Knoldus Inc.
 
REST API
REST API
Tofazzal Ahmed
 
Spring Security
Spring Security
Knoldus Inc.
 
Angular 9
Angular 9
Raja Vishnu
 
Introducing Swagger
Introducing Swagger
Tony Tam
 
APIsecure 2023 - Exploring Advanced API Security Techniques and Technologies,...
APIsecure 2023 - Exploring Advanced API Security Techniques and Technologies,...
apidays
 
Angularjs PPT
Angularjs PPT
Amit Baghel
 
Swagger UI
Swagger UI
Walaa Hamdy Assy
 
Best PHP Frameworks
Best PHP Frameworks
Clixlogix Technologies
 
Git and github
Git and github
Sayantika Banik
 
What is Angular?
What is Angular?
Albiorix Technology
 
Understanding REST APIs in 5 Simple Steps
Understanding REST APIs in 5 Simple Steps
Tessa Mero
 
Dapper
Dapper
Suresh Loganatha
 

Similar to API Development with Laravel (20)

Ultimate Laravel Performance Optimization Guide
Ultimate Laravel Performance Optimization Guide
CMARIX TechnoLabs
 
Building RESTful APIs with Laravel A Complete Guide.pdf
Building RESTful APIs with Laravel A Complete Guide.pdf
Grey Space Computing
 
What is the Role of Laravel in API Development?
What is the Role of Laravel in API Development?
Acquaint Softtech Private Limited
 
Why Laravel is the Best Choice for Developing RESTful APIs?
Why Laravel is the Best Choice for Developing RESTful APIs?
Acquaint Softtech Private Limited
 
Integrating Third-party APIs with Laravel.pptx
Integrating Third-party APIs with Laravel.pptx
Surekha Technologies
 
Test driven APIs with Laravel
Test driven APIs with Laravel
Michael Peacock
 
How to Create REST API Using Laravel Framework
How to Create REST API Using Laravel Framework
Marrie Morris
 
Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)
Viral Solani
 
Laravel Restful API and AngularJS
Laravel Restful API and AngularJS
Blake Newman
 
Building Awesome APIs with Lumen
Building Awesome APIs with Lumen
Kit Brennan
 
Building restful apis with laravel
Building restful apis with laravel
Mindfire LLC
 
Laravel 4 presentation
Laravel 4 presentation
Abu Saleh Muhammad Shaon
 
Building Scalable Applications with Laravel
Building Scalable Applications with Laravel
Muhammad Shakeel
 
Object Oriented Programming with Laravel - Session 4
Object Oriented Programming with Laravel - Session 4
Shahrzad Peyman
 
What's New In Laravel 5
What's New In Laravel 5
Darren Craig
 
SDPHP Lightning Talk - Let's Talk Laravel
SDPHP Lightning Talk - Let's Talk Laravel
marcusamoore
 
Service-Oriented Architecture
Service-Oriented Architecture
Samantha Geitz
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher Pecoraro
Christopher Pecoraro
 
Laravelcollectionsunraveled
Laravelcollectionsunraveled
Renato Lucena
 
SWD 414 BackdnIIgjfjjtuutfyutryytyiy.pptx
SWD 414 BackdnIIgjfjjtuutfyutryytyiy.pptx
YahayaAbubakartakala
 
Ultimate Laravel Performance Optimization Guide
Ultimate Laravel Performance Optimization Guide
CMARIX TechnoLabs
 
Building RESTful APIs with Laravel A Complete Guide.pdf
Building RESTful APIs with Laravel A Complete Guide.pdf
Grey Space Computing
 
Integrating Third-party APIs with Laravel.pptx
Integrating Third-party APIs with Laravel.pptx
Surekha Technologies
 
Test driven APIs with Laravel
Test driven APIs with Laravel
Michael Peacock
 
How to Create REST API Using Laravel Framework
How to Create REST API Using Laravel Framework
Marrie Morris
 
Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)
Viral Solani
 
Laravel Restful API and AngularJS
Laravel Restful API and AngularJS
Blake Newman
 
Building Awesome APIs with Lumen
Building Awesome APIs with Lumen
Kit Brennan
 
Building restful apis with laravel
Building restful apis with laravel
Mindfire LLC
 
Building Scalable Applications with Laravel
Building Scalable Applications with Laravel
Muhammad Shakeel
 
Object Oriented Programming with Laravel - Session 4
Object Oriented Programming with Laravel - Session 4
Shahrzad Peyman
 
What's New In Laravel 5
What's New In Laravel 5
Darren Craig
 
SDPHP Lightning Talk - Let's Talk Laravel
SDPHP Lightning Talk - Let's Talk Laravel
marcusamoore
 
Service-Oriented Architecture
Service-Oriented Architecture
Samantha Geitz
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher Pecoraro
Christopher Pecoraro
 
Laravelcollectionsunraveled
Laravelcollectionsunraveled
Renato Lucena
 
SWD 414 BackdnIIgjfjjtuutfyutryytyiy.pptx
SWD 414 BackdnIIgjfjjtuutfyutryytyiy.pptx
YahayaAbubakartakala
 
Ad

More from Michael Peacock (20)

Immutable Infrastructure with Packer Ansible and Terraform
Immutable Infrastructure with Packer Ansible and Terraform
Michael Peacock
 
Symfony Workflow Component - Introductory Lightning Talk
Symfony Workflow Component - Introductory Lightning Talk
Michael Peacock
 
Alexa, lets make a skill
Alexa, lets make a skill
Michael Peacock
 
An introduction to Laravel Passport
An introduction to Laravel Passport
Michael Peacock
 
Phinx talk
Phinx talk
Michael Peacock
 
Refactoring to symfony components
Refactoring to symfony components
Michael Peacock
 
Dance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech Talk
Michael Peacock
 
Powerful and flexible templates with Twig
Powerful and flexible templates with Twig
Michael Peacock
 
Introduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
Vagrant
Vagrant
Michael Peacock
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
Michael Peacock
 
Evolution of a big data project
Evolution of a big data project
Michael Peacock
 
Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012
Michael Peacock
 
Dealing with Continuous Data Processing, ConFoo 2012
Dealing with Continuous Data Processing, ConFoo 2012
Michael Peacock
 
Data at Scale - Michael Peacock, Cloud Connect 2012
Data at Scale - Michael Peacock, Cloud Connect 2012
Michael Peacock
 
Supermondays twilio
Supermondays twilio
Michael Peacock
 
PHP & Twilio
PHP & Twilio
Michael Peacock
 
PHP Continuous Data Processing
PHP Continuous Data Processing
Michael Peacock
 
PHP North East Registry Pattern
PHP North East Registry Pattern
Michael Peacock
 
PHP North East - Registry Design Pattern
PHP North East - Registry Design Pattern
Michael Peacock
 
Immutable Infrastructure with Packer Ansible and Terraform
Immutable Infrastructure with Packer Ansible and Terraform
Michael Peacock
 
Symfony Workflow Component - Introductory Lightning Talk
Symfony Workflow Component - Introductory Lightning Talk
Michael Peacock
 
Alexa, lets make a skill
Alexa, lets make a skill
Michael Peacock
 
An introduction to Laravel Passport
An introduction to Laravel Passport
Michael Peacock
 
Refactoring to symfony components
Refactoring to symfony components
Michael Peacock
 
Dance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech Talk
Michael Peacock
 
Powerful and flexible templates with Twig
Powerful and flexible templates with Twig
Michael Peacock
 
Introduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
Michael Peacock
 
Evolution of a big data project
Evolution of a big data project
Michael Peacock
 
Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012
Michael Peacock
 
Dealing with Continuous Data Processing, ConFoo 2012
Dealing with Continuous Data Processing, ConFoo 2012
Michael Peacock
 
Data at Scale - Michael Peacock, Cloud Connect 2012
Data at Scale - Michael Peacock, Cloud Connect 2012
Michael Peacock
 
PHP Continuous Data Processing
PHP Continuous Data Processing
Michael Peacock
 
PHP North East Registry Pattern
PHP North East Registry Pattern
Michael Peacock
 
PHP North East - Registry Design Pattern
PHP North East - Registry Design Pattern
Michael Peacock
 
Ad

Recently uploaded (20)

War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
Precisely
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
Precisely
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 

API Development with Laravel

Editor's Notes

  • #3: This talk is going to walk through the creation of a simple API using Laravel. We will build a very basic API for a user group or conference, with support for events, talks, speakers and reviews. We will start out by setting up a fresh Laravel project, creating some models, migrations and seeders, so that we can have some data to play around with right away. We will then take a look at Laravels routing, and how it differentiates API and web requests, and how we can customise this. We will build some API controllers to quickly process an inbound request and return a response. We will customise the response using API resources, and finally we will look at how to enable authentication within the API with Laravel Passport, and some caveats to be aware of.
  • #4: I’m a freelance developer and consultant CTO mainly helping early stage SaaS start-ups to build products and teams. I’m one of the organisers of PHPNE – a PHP user group NE UK.
  • #5: In order for us to build an API, we need a basic laravel project, and some models.
  • #6: The first step is for us to create a new project, which we can quickly do using composer.
  • #7: We want to model our events, talks, speakers and reviews, so lets start with a migration to store that data in the database. Caveat: Some developers advocate creating a migration per new model (and there are some benefits to that which I’ll come to in a moment), but I often find with a new project, especially if there are related models with cyclical relationships, it’s a little easier to do it all in one go. Jump to the migration.
  • #8: Events, and sponsors.
  • #9: Speakers and event sponsors.
  • #10: Talks and reviews. Note on reviews – intentionally allowing user and non-user reviews for the purposes of dynamic API response
  • #12: We also want to create a model to represent the data in our application. The factory flag also tells Laravel we want to make a factory, which is used to quickly create fake instances of this model for testing or seeding purposes. We could also use the –m flag to tell Laravel to create a migration at the same time. Jump to the model, and then the factory.
  • #13: Scaffolded model
  • #14: Fleshed out model
  • #15: Scaffolded Factory
  • #16: Fleshed out factory
  • #17: We can then make a seeder which will be used to populate a fresh install of the project with data.
  • #18: Seed scaffolding
  • #19: Implemented seeder
  • #20: Once we have seeders in place and tied to our database seeder, we can then seed our database with data. Personally, if I have live seed data, such as pre-defined categories, countries, currencies or other things which a system needs from day 1, I’d put them in a migration, and use the seeder purely for seeding a test or development database.
  • #21: Laravel has a concept of a resource controller, which is a controller scaffolded out for list, create, edit, delete, view operations on a particular model. They have methods for index, create, store, edit, update and destroy. Edit and Create are methods to show the create and edit form and in the context of an API they don’t serve a purpose. To make our lives even easier still, Laravel has support for what it calls API controllers, which essentially doesn’t scaffold these surplus methods.
  • #22: Creating an API controller involves the make:controller command, with the name of the class we want to create, the model we want to bind it to, and the api option to tell it that this resource controller is an API controller.
  • #23: API Controller scaffolding
  • #24: As we want to be able to create events and other things in our system, we need a request class to validatate the users request. The make:request command will create a request class for us, and then we just fill it out, and type hint it to the controller action where we want to use it.
  • #25: Request scaffolding. Implementation follows.
  • #26: Implemented rules
  • #27: Resource and API controllers can scaffolded to be nested under a single parent, by providing the parent model via the parent flag. In this instance, the controller will be scaffolded so that the methods are passed the parent model before the child model. The scaffolding is limnited to one parent, however if you are manually creating API controllers you can nest further down if you like.
  • #28: API Controller with model and parent, scaffolding
  • #30: One useful concept with Laravel when it comes to routes is middleware. Middleware are specific blocks of code which run once a route is matched, but before control is delegated to the matching route. In some cases they are purely used to modify the request, such as the bindings middleware are used to bind our models to our route parameters. In other cases, the middleware might reject a request – such as the CSRF middleware, which checks for the existance of a correct CSRF token. Useful middleware is pre-configured and grouped into middleware groups, which means that middleware which is useful in the context of a web request is automatically included on web routes, and API related middleware is automatically included on the API routes.
  • #31: By default, we have the throttle middleware enabled. The format of this configuration is 60 requests per minute. If someone exceeds this limit, then their requests will be throttled.
  • #32: Dynamic rate limiting allows us to customize the limit based upon an attribute associated with the authenticated user. This example will limit requests based off the users rate_limit parameter per minute. This is new in Laravel 5.6.
  • #33: Rate limiting information is helpfully returned to the user in the response headers. Here you can see we have a limit of 60 requests, and we have 57 requests remaining.
  • #34: Once we hit the limit, Laravel will automatically reject the request with a 429 response, and also returns a timestamp for when our rate limit will be reset.
  • #35: By default, the route service provider does some helpful setup for our API routes. It tells Laravel that the routes should all be prefixed with /api, that they should run the API middleware which we looked at before, that the controllers are within a particular namespace and that they have their own routes file.
  • #36: Given we probably want to have our API controllers in a separate location to our web controllers, it makes sense to change the namespace to look in the API directory.
  • #37: Since we have built some resource / API controllers, we can enable them with the resource route helper, simply pointing to the controller, and specifying the route prefix we want to use.
  • #38: So far we haven’t really done much API specific work. We have chosen some API routes, we are returning models instead of views, and we are omitting some standard CRUD methods which would typically display a form. That’s the only differences we have made so far with regards to what we have put together. Lets see what this looks like.
  • #52: Scaffolded event resource
  • #53: Using the resource.
  • #54: This is the DEFAULT resource view, just all of the properties, excluding hidden ones. However we can customise the resource. Example of a customised one on the following slide.
  • #55: While the resource doesn’t extend our model, it has access to the models properties as if they were its own. We just access them in the toArray method
  • #56: By default, Laravel wraps the response under an attribute called data. This is primarily because with older browsers there are security issues with exposing API data as the root attributes in your response (I’m not sure but this might be limited to arrays, and not objects). In this situation, with older browsers, its possible for a third party site to load in your API via a JS tag and extract data, which if for some reason your API maintained authenticated state (or maybe its an AJAX response which allows authenticated state) – subsequently stealing data from the API. By having this as an object, it isn’t possible to do this. If we return a collection by default, then this would be an array, so the data wrapping, keeps our data out of the root response. The withoutWrapping method only affects the outer-most response and will not remove data keys that you manually add to your own resource collections. Pagination will ignore this, because there is meta data the collection has to be one level in, in order for the meta data and links to be stored
  • #58: We can also customise the data wrapping if we want, changing the word data for something else. Useful if you are porting over a legacy API, or just want a more descriptive response.
  • #73: We can also chose to customise the meta data, and other root level elements, for situations where the resource being returned is the root level resource using with with() method. This adds data to the response, but only if it isn’t a nested resource.
  • #75: Earlier, we noticed how the (non resource) response would vary depending on how related models were loaded. If we want to maintain this approach, we can use conditionals in our responses.
  • #88: We can also put this in our Request class (maybe jump to the code), within there we can check to see if there is a user associated with the request, if there is we check that the user has the correct permissions. If not then we don’t let them review.