Giter Site home page Giter Site logo

tony133 / laravel-api-rest Goto Github PK

View Code? Open in Web Editor NEW
217.0 11.0 91.0 415 KB

Simple example of a API REST with Laravel 9.x

License: MIT License

PHP 83.34% Blade 16.37% JavaScript 0.30%
api api-rest php laravel laravel-application laravel9 rest rest-api

laravel-api-rest's Introduction

Laravel Api Rest

Build Status

Simple example of a REST API with Laravel 9.x

Install with Composer

    $ curl -s http://getcomposer.org/installer | php
    $ php composer.phar install or composer install

Set Environment

    $ cp .env.example .env

Set the application key

   $ php artisan key:generate

Run migrations and seeds

   $ php artisan migrate --seed

Getting with Curl

    $ curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X GET http://127.0.0.1:8000/api/books
    $ curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X GET http://127.0.0.1:8000/api/books/:id
    $ curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X POST -d '{"title":"Foo bar","price":"19.99","author":"Foo author","editor":"Foo editor"}' http://127.0.0.1:8000/api/books
    $ curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X PUT -d '{"title":"Foo bar","price":"19.99","author":"Foo author","editor":"Foo editor"}' http://127.0.0.1:8000/api/books/:id
    $ curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X DELETE http://127.0.0.1:8000/api/books/:id

Pagination with Curl

    $ curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X GET http://127.0.0.1:8000/api/books?page=:number_page  -H 'Authorization:Basic username:password or email:password'

User Authentication with Curl with middleware auth.basic.username

    $ curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X GET http://127.0.0.1:8000/api/books  -H 'Authorization:Basic username:password'

User Authentication with Curl using middleware auth.basic

    $ curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X GET http://127.0.0.1:8000/api/books  -H 'Authorization:Basic email:password'

Run App in container Docker with Laravel Sail

docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v $(pwd):/opt \
    -w /opt \
    laravelsail/php81-composer:latest \
    composer install --ignore-platform-reqs

Run Start Application with Laravel Sail

  ./vendor/bin/sail up

laravel-api-rest's People

Contributors

akeren avatar dependabot[bot] avatar muhammetali avatar tony133 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-api-rest's Issues

can I install this code with composer?

Hi,
I wrote some codes in laravel and want to use your code. Can I use this code with composer or any other solution for using your code inside my project?
thanks,

Asking for help

Would you kindly help me with how to test this API in POSTMAN?
Thank you!

Entity relationships

Hello,

I'm using your repo as a guide to make an API and I have a doubt. Hope you can help me.

When you have a related entity. For example, if the Book Author is another Author model. What's the best approach?
Now what I'm doing is in the BookResource, use 'author' => new AuthorResource( $this->author ). It works, and it shows the information, but I suposse I'll have problems with recursivity easily.

Is there any feature like a "link"? I mean, in author show a link to the resource to get the author data like "API URL/authors/12".

Many thanks

Proposal

Hello, I have created a tool that automatically generates unit tests of APIs created with Laravel.
I have analyzed this project and I would like you to try the tool with it so that you give me your opinion. I think that it's perfect to test my tool. It will not take you long and I would be very grateful.

username:password should be send base64-encoded (Minor detail on info/read.me)

Hi, thnx for this great nice restapi setup :-D Saved me a bunch of time ...

Though there is one minor thing, for the Curl with Authentication you should not send out plain username/email and password but base64 encode it.

Took me 10 minutes to fiddle why I got "Invalid credentials." all the time..

So basicly:
curl -H 'content-type: application/json' -v -X GET http://localhost:8000/api/books -H 'Authorization:Basic bWljaGllbEBhdWVyYmFjaC5ubDpXZWxjb21lMTIzNDU='

:-D

Keep up the great work!

Michiel

Passport

Congratulations, very good job.
Just one question, in this example you do not use the passport, right?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.