Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Nick Frostbutter
Nick Frostbutter

Posted on • Originally published atfrostbutter.com on

     

Create a custom Laravel Valet driver

Create a custom Laravel Valet driver

Valet is the best! I mean it. Perfect for the a solid, lightweight, and minimal development environment. You can link your project's directory then browse to the project using a ".test" domain. So clean.

The problem that I faced was that I don't use Laravel. A sin for a php developer I know. So in order to be able to use Valet style ".test" domains for my custom web apps, I must create a custom Valet driver for your app. Luckily, if you write it once (or use mine) then you will be set until Valet or Laravel changes a bunch. So basically, forever 🤞

What is a Valet driver?

Just a single class. Specifically a custom class that extends the "LaravelValetDriver" class. The goal of these custom drivers enables developers to use Valet for other web apps or CMS that are not natively supported in Valet.

You can use these drivers to specifically handle the routes needed for your web app on your local dev environment to speed up and betterify (real word) your dev life.

Where to save your custom Valet driver?

Really you have a few options. Most of the default drivers that come with Valet are stored inside of the local default folder at~/.config/valet/Drivers. This is where you can save all of your custom, reusable drivers. They will be automatically loaded by Valet when pages are rendered. No more setup needed.

You can also add the unique driver into your web app's working directory. Say you are building you app in yourC:\xampp\htdocs\mysweetapp. You place the custom driver inside the root of your apps folder. It However, it must be named "LocalValetDriver.php". When you link this folder to Valet, it will automatically use this local driver for your site "mysweetapp".

For this article, I will be doing the second option.

Create a file named LocalValetDriver.php

This local valet driver will be loaded only for the current application. No more, no less.

classLocalValetDriverextendsLaravelValetDriver{/** your code here */}
Enter fullscreen modeExit fullscreen mode

Since we extend the built in Laravel class namedLaravelValetDriver , we are required to have 2 functions.

  • serves(), and
  • frontControllerPath()

"serves" function

Theserves() function is used to determine if the current web app should use the valet driver. It receives 3 parameters returns true or false. Ifserves() returns atrue, then Valet will attempt to use the driver to actually load and process your website. We want true.

  • $sitePath : the absolute path that your linked site inside of valet (e.g.C:\xampp\htdocs\mysweetapp)
  • $siteName : the name that your valet site (e.g.mysweetapp); the same name used for your .test site (e.g.mysweetapp.test)
  • $uri : the actual uri from the browser request; say you visit mysweetapp.test/contact, the uri is "contact". If you browse tomysweetapp.test, the uri isempty

The logic inside the function is simple. We check for specific files that are specific to the web app or CMS you want to use this custom driver for. WordPress for example would check for wp-config.php.

/*** Determine if the driver serves the request.** @param string $sitePath* @param string $siteName* @param string $uri* @return bool*/publicfunctionserves($sitePath,$siteName,$uri){return(file_exists($sitePath.'/init.php')&&file_exists($sitePath.'/config.ini'));}
Enter fullscreen modeExit fullscreen mode

For my app, I am checking for 2 files:init.php * andconfig.ini. These files then would need to exists inside If they exists my working directory,C:\xampp\htdocs\mysweetapp\this-file-here.php. If they are both found, then the driver will be loaded and used by Valet.

"frontControllerPath" function

The frontControllerPath function returns the full absolute path of the page being loaded on your website. Fully resolved, as they say. This function is the heart of your custom driver. It will perform all the logic to check if the file exists. Even use it to hide php file extensions with Valet (like I do).

ThefrontControllerPath() function takes the same parameters as theserves() function. The exact same. How convenient.

In my custom driver's function, I first check if the the request uri is the root of a directly (e.g.mysweetapp.test) and loading the index.php file by default.

Next checking if the request uri is an actual file that exits with the given extension (e.g. mysweetapp.test/page1.php).

Then checking if the the uri exists with the added php extension (e.g.mysweetapp.test/page_name turns intopage_name.php). This will effectivelyhide php extensions inside of Valet. I love it and use it for every web app.

Finally defaulting to the 404 page that is located inside thepublic_html directory. If no 404.php file exists, then it will default to the default Valet 404 page.

/**    * Get the fully resolved path to the application's front controller.    * @param string $sitePath    * @param string $siteName    * @param string $uri    * @return string*/publicfunctionfrontControllerPath($sitePath,$siteName,$uri='index.php'){// add the public folder to the sitePath$sitePath.='/public_html';// smart handle the requests, defaulting to index.php in directories and hiding php extensionsif(is_dir($sitePath.$uri)&&file_exists($sitePath.$uri.'/index.php'))return$sitePath.$uri.'/index.php';elseif(file_exists($sitePath.$uri))return$sitePath.$uri;elseif(file_exists($sitePath.$uri.'.php'))return$sitePath.$uri.'.php';elsereturn$sitePath.'/404.php';}
Enter fullscreen modeExit fullscreen mode

Complete custom Valet driver for a custom web app

The complete working version of my custom Valet driver is below. Of course feel free to modify it to your hearts content. Source available on GitHub.

<?php/*** Local valet driver for a custom framework or web app* by Nick Frostbutter (https://frostbutter.com)* - loads index.php file by default in all directories* - hides "php" extensions by default* Tutorial: https://frostbutter.com/articles/create-a-custom-laravel-valet-driver-hide-php-extensions*/classLocalValetDriverextendsLaravelValetDriver{/**    * Determine if the driver serves the request.    *    * @param string $sitePath    * @param string $siteName    * @param string $uri    * @return bool    */publicfunctionserves($sitePath,$siteName,$uri){return(file_exists($sitePath.'/init.php')&&file_exists($sitePath.'/config.ini'));}/**    * Get the fully resolved path to the application's front controller.    *    * @param string $sitePath    * @param string $siteName    * @param string $uri    * @return string    */publicfunctionfrontControllerPath($sitePath,$siteName,$uri='index.php'){// add the public folder to the sitePath$sitePath.='/public_html';// smart handle the requests, defaulting to index.php in directories and hiding php extensionsif(is_dir($sitePath.$uri)&&file_exists($sitePath.$uri.'/index.php'))return$sitePath.$uri.'/index.php';elseif(file_exists($sitePath.$uri))return$sitePath.$uri;elseif(file_exists($sitePath.$uri.'.php'))return$sitePath.$uri.'.php';elsereturn$sitePath.'/404.php';}}?>
Enter fullscreen modeExit fullscreen mode

This driver has been a delight, just like Valet. Making your life as a developer easier is a must.

If you are interested, you can read more about custom Valet drivers from theLaravel docs here.


This article onhow to create a custom laravel valet driver was originally by me,Nick Frostbutter, on my personal site.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
eddiecody profile image
EddieCody
I am highly adept at vaisual strategy. Layout development, branding, and new media advertising, like breaking a binding spel
  • Location
    Miami, FL 33126
  • Work
    Designer
  • Joined

If it does, can you please provide additional code information to explain what's happening?breaking a binding spell

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Indie developer and maker
  • Joined

More fromNick Frostbutter

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp