Coding

Setup AstroNvim for PHP and Laravel the Easy Way

Posted on Fri, Jan 17, 2025

Setup AstroNvim for PHP and Laravel the Easy Way

I've been using Neovim as my daily driver for a while now and thought I'd share a straightforward guide to setting up Neovim for Laravel development using AstroNvim.

Installing AstroNvim

First, let's get Neovim set up on your machine. We'll start by heading over to the AstroNvim Documentation and following the installation instructions. Here's a step-by-step guide to make it easier:

Backup Your Existing Neovim Configuration

If you already have a Neovim configuration, back it up by running:

mv ~/.config/nvim ~/.config/nvim.bak

Clear Neovim Data (Optional but Recommended)

Clear out existing Neovim data to ensure a clean slate:

mv ~/.local/share/nvim ~/.local/share/nvim.bak
mv ~/.local/state/nvim ~/.local/state/nvim.bak
mv ~/.cache/nvim ~/.cache/nvim.bak

Clone and Install AstroNvim

Now, let's install AstroNvim:

git clone --depth 1 https://github.com/AstroNvim/template ~/.config/nvim
rm -rf ~/.config/nvim/.git
nvim

When you open Neovim for the first time, AstroNvim will set itself up. Once it's done, close Neovim.


Configuring AstroNvim for PHP and Laravel

Next, we'll configure AstroNvim with plugins that are particularly useful for PHP and Laravel development.

Import Plugins from the AstroNvim Community

AstroNvim has a community plugins repository that we'll leverage. Open Neovim in the terminal, and the Lazy Installer will automatically pop up to install the default plugins. Let it finish, then close Neovim.

To start customizing, open your configuration folder by running:

nvim ~/.config/nvim

If you don’t see the file tree on the left, press the leader key (space by default) followed by e to toggle it.

Add Plugin Imports

Edit the community.lua file to include the plugins you need. Add the following:

-- AstroCommunity: import any community modules here

---@type LazySpec
return {
  "AstroNvim/astrocommunity",
  { import = "astrocommunity.pack.blade" },
  { import = "astrocommunity.pack.html-css" },
  { import = "astrocommunity.pack.json" },
  { import = "astrocommunity.pack.lua" },
  { import = "astrocommunity.pack.laravel" },
  { import = "astrocommunity.pack.markdown" },
  { import = "astrocommunity.pack.php" },
  { import = "astrocommunity.pack.tailwindcss" },
  { import = "astrocommunity.colorscheme.onedarkpro-nvim" },
}

After saving, run :Lazy in Neovim and press Shift+i to install the new plugins.


Blade Support and LSP

To enhance support for Blade files, create a new file called blade.lua in the plugins folder:

nvim ~/.config/nvim/lua/plugins/blade.lua

Add the following content:

return {
  {
    "jwalton512/vim-blade",
    ft = { "blade" }, -- Load only for Blade files
  },
}

Save and exit. Then, open Lazy again (:Lazy) and press Shift+i to install this plugin.


Additional Configuration

Enable Core Files

AstroNvim provides several pre-configured files for LSP, UI, and more. To activate them, remove the first line from the following files:

if true then return {} end -- WARN: REMOVE THIS LINE TO ACTIVATE THIS FILE

The files are located in:

  • astrocore.lua
  • astrolsp.lua
  • astroui.lua
  • mason.lua
  • treesitter.lua
  • user.lua

Update mason.lua for Additional LSPs

To configure LSP servers, edit the mason.lua file and add any required servers. Here’s an example with some PHP-specific tools:

-- Customize Mason plugins

---@type LazySpec
return {
  {
    "williamboman/mason-lspconfig.nvim",
    opts = {
      ensure_installed = {
        "lua_ls",
        "stimulus_ls",
        "dockerls",
        "intelephense",
        "spectral",
      },
    },
  },
  {
    "jay-babu/mason-null-ls.nvim",
    opts = {
      ensure_installed = {
        "stylua",
      },
    },
  },
  {
    "jay-babu/mason-nvim-dap.nvim",
    opts = {
      ensure_installed = {
        "python",
      },
    },
  },
}

Install the configured LSPs and tools using Lazy (:Lazy) if prompted.


Wrapping Up

With these steps, you now have a robust Neovim setup tailored for Laravel development. To explore my complete configuration, visit my GitHub repository.

Happy coding!