Giter VIP home page Giter VIP logo

codespaces-test-laravel's Introduction

How To Build a Laravel + MongoDB Back End Service

This code was written to accompany this tutorial article.

Prerequisites

You'll need the following installed on your computer to follow along with this tutorial:

  • A MongoDB Atlas cluster
  • A GitHub account if you want to use GitHub Codespaces (a 1-click experience)
    • The master repo has all the code, but we also have a starter branch if you want to follow the project from scratch and build the migrations etc.
  • A code editor of your choice for local development

The article mentions several ways to get a Laravel development environment up and running.

πŸš€ Launch this repo in CodeSpaces

⏳Codespaces will build the app's container(s). This may take ~3 minutes.

βœ…Done! We now have our project running inside CodeSpaces. We can proceed to setting up Laravel

πŸ‘‹ Before you run this Laravel app

1. Final Laravel app setup

After cloning the code repo or launching a Docker/CodeSpaces instance, a script called init_repo.sh will be automatically executed (as setup in devcontainer.json) to:

  • install dependencies via Composer
  • create a new .env file
  • generate a new Laravel App Key
  1. All you need to do is to add your MongoDB credentials in Laravel's .env file, using the MONGODB_URI environment variable. Here's how to get your credentials It looks something like this:
MONGODB_URI=mongodb+srv://USERNAME:[email protected]/?retryWrites=true&w=majority

❗Note that this branch already has the Laravel Model and Migrations already created and ready, but the tables have been initialized yet.

  1. You can test your credentials by using the code's API endpoint
<siteroot>/api/ping/

Find the site's root URL by going to the "Ports" tab and click on the globe icon of port 80

  1. If the MongoDB ping test worked, use this command in the terminal to initialize the tables

php artisan migrate:refresh

2. Ready!

Our base Laravel app is ready πŸ₯³.

Next, try some of the things we talked about in our How To Build a Laravel + MongoDB Back End Service

πŸš€ Launch locally with Docker

Assuming that you already have Docker Desktop installed on Windows/Mac or Docker on Linux,

  • clone the repository to a local directory
  • navigate to the ./devcontainer folder
  • execute docker compose up
  • in the PHP container, execute sh init_repo.sh
  • initialize your .env file as instructed above

Once the container(s) are up, visit http://localhost

Optional: Xdebug

The xdebug.php-debug VS Code extension is automatically installed if you launch via devcontainer.json.

πŸ‘€ Important: our .devcontainer/.docker/php/xdebug.ini file is setup by default with xdebug.client_host=localhost, which should works for CodeSpaces and Devcontainers.

For local development, you need to replace localhost with the IP where your code IDE runs or a dns name that maps to it. That's because your PHP container and the IDE host tend to have different IPs.

If you are using our container directly (docker compose up), or via VS Code (devcontainer), we suggest the following Xdebug configs visual studio. Note the difference in path mapping.

CodeSpaces and (inside a Devcontainer)

{
  "name": "Listen for Xdebug",
  "type": "php",
  "request": "launch",
  "port": 9003,
  "pathMappings":  {
    "/var/www/htdoc": "${workspaceFolder}"
  }
},

local development with Docker

The debug config file is located in <repository_dir>/.vscode/launch.json

{
  "name": "Listen for Xdebug",
  "type": "php",
  "request": "launch",
  "port": 9003,
  "pathMappings":  {
    "/var/www/htdoc": "${workspaceFolder}/src"
  }
},

Disclaimer

Use at your own risk; not a supported MongoDB product

codespaces-test-laravel's People

Contributors

hubertnguyen avatar gromnan avatar

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.