تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Introducing Valet drivers0:00

We know that every time you request a .test site, Valet grabs the contents of the matching directory and serves it up to you. But how does Valet know how to serve it? And what if it's not a Laravel site? What if it's WordPress or Jigsaw or Magento, which don't all have a public slash index.php file? What does Valet do with it then? Let's take a dive into the wonderful world of Valet drivers. Valet drivers are a series of PHP classes, each of which attempt to represent a type of website Valet can serve.

Valet drivers are a series of php classes, each of which attempt to represent a type of website Valet can serve. Laravel, static site, WordPress, you name it. Now, if you visit any folder in your local machine in the terminal and you run valet which, Valet will tell you which driver should serve this folder. You might get basic Valet driver or Laravel Valet driver or any other of a host of possibilities. Now, once Valet has matched your local site folder with a driver, it then asks this driver how it should serve the site.

Now, once Valet has matched your local site folder with a driver, it then asks this driver how it should serve the site. So if it's a Laravel, it will say something like this. Serve any static files like CSS or JavaScript out of the public directory. So if you get any requests, for example, mysite.com/style.css, the Laravel driver will say look for that in public/style.css. And then it'll say route all other requests. So if there's no matching CSS or JavaScript file or other static file, they should all route to public/index.php, which is the front controller of Laravel.

they should all route to public/index.php, which is the front controller of Laravel. Now, let's say you have a site that's for Jigsaw, which is a static site generator. In this case, its driver will say look for the file or folder under build/local. Because with Jigsaw, you go build/local/index.html or build/local/about/index.html. It's a perfect map. So with Jigsaw sites, you look for anything under build_local. And with all the rest of the drivers, it's that same kind of matching.

So with Jigsaw sites, you look for anything under build_local. And with all the rest of the drivers, it's that same kind of matching. With this specific type of site, how do I match a URI to the local files that should be serving it? Out of the box, Valet comes with 22 different drivers, each made for serving a different type of static or PHP-based website. And each of these classes here has two jobs. First, to identify whether it should serve a given site. And second, if it should, to tell Valet how to serve that site. So let's take a quick look at the Laravel Valet driver to see what it's actually doing.

Driver methods overview2:09

And second, if it should, to tell Valet how to serve that site. So let's take a quick look at the Laravel Valet driver to see what it's actually doing. First, we have the serves method. Given a path for a site and also the name in the URI in case they help, the driver returns a Boolean, whether or not it should serve this site. Every single site that Valet tries to serve is passed through all the drivers until it can find one that says, yep, I'm supposed to serve that. Next, we have three methods that teach Valet how to serve the site. BeforeLoading, isStaticFile, and frontControllerPath. BeforeLoading is an optional method,

BeforeLoading, isStaticFile, and frontControllerPath. BeforeLoading is an optional method, which allows us to define any prep work we need to do to get ready for serving this type of site. It's most common that we set environment or super globals here. isStaticFile determines whether a given URI should be served as a static file. If so, it'll return the path to that file. If not, it'll just return false. frontControllerPath returns the path to the primary front controller, or basically the main index file that handles all requests for the framework.

frontControllerPath returns the path to the primary front controller, or basically the main index file that handles all requests for the framework. If you're using static HTML, there is no front controller. But if you're using a framework like Laravel, it's very common for all requests that aren't matching static files to route through a single file, and that single file is called the front controller, and that's what allows you to define routing in your framework. There's one more method we might want to use in our drivers, although this particular driver doesn't use it,

There's one more method we might want to use in our drivers, although this particular driver doesn't use it, and it's called ComposerRequires. It's often useful in the serves method. You can say, does this project have Statamic and composer.json? If so, it's a Statamic project. This is all great if you want to use one of those 22 existing project types. But what if your favorite tool or framework isn't present? Or what if you're working with a customized form or one of those project types? In that case, you want to build your own custom driver.

Creating a custom driver3:42

Or what if you're working with a customized form or one of those project types? In that case, you want to build your own custom driver. Thankfully, building your own driver is straightforward. First, let's open up the directory for custom drivers, ~/.config/valet, which is our valet config directory, and then let's go to the drivers subdirectory. As you can see, valet already ships with a sample custom driver just to show you how this works. Just like the one we already looked at, it's got a serves method, it's got a isStaticFile method,

Just like the one we already looked at, it's got a serves method, it's got a isStaticFile method, and it's got a frontControllerPath method. They're simple. It just assumes the frontControllerPath is public/index.php. It looks for the static files under the public directory, and it gives you a placeholder to show what it might look like to identify your particular framework in the serves method. So let's make a copy of that driver and rename it for our project. We'll assume for now that I've made a customization to the Floga app.

Modifying driver for assets4:30

So let's make a copy of that driver and rename it for our project. We'll assume for now that I've made a customization to the Floga app that moves its front-end assets to the front-end directory instead of the public directory. So we'll just call this one FlogaValetDriver. So now let's open up the file and make some modifications. Since we're making a modified Laravel driver, let's actually grab the contents of the default Laravel driver and just modify that. I still have it open here, so let's just copy it.

and just modify that. I still have it open here, so let's just copy it. But you notice this is going to be ValetDriversCustom, so we've got to change that. So ValetDriversCustom. We'll rename this to FlogaValetDriver. Since we're only changing how to serve static files, we could actually just extend the Laravel ValetDriver here. And that way, any methods we're not changing, we can just delete. By default, this driver says,

And that way, any methods we're not changing, we can just delete. By default, this driver says, if the static file exists under the public directory, serve it. So we're going to work with that, but just modify it to be a different directory. So any place you see public here, which is by default public, but also any links to the storage/app/public directory, just for fun, we're going to rename those to front-end for this particular driver. So this is exactly the same as the Laravel ValetDriver.

for this particular driver. So this is exactly the same as the Laravel ValetDriver. It just looks in /front-end instead of /public for your assets. And if you've done the same link, it looks in storage/app/front-end instead of storage/app/public. And that's it. Now, a little bit earlier, I went to our Floga directory, and I added a front-end folder. And in there, you can see I added test.css.

and I added a front-end folder. And in there, you can see I added test.css. That file right there just has dot this is colon fake, and that's it. Now, traditionally, with the Laravel ValetDriver, that wouldn't serve because nothing in the front-end directory should serve. But now that we've added our Floga ValetDriver, this is fake. Now, of course, you have to be careful because what we just did was we now overrode this for all Laravel apps. So all the Laravel apps that we're hosting are broken.

Scoping driver to site6:43

we now overrode this for all Laravel apps. So all the Laravel apps that we're hosting are broken because they're all going to try and serve from front-end instead of serving from public. So we can find a unique way to determine whether a particular site should be served by this driver. Whatever we do in here, if it returns yes, that means it's Floga. Now, remember the default for the Laravel ValetDriver, just checks whether public/index.php exists and whether artisan exists. So we can use those.

just checks whether public/index.php exists and whether artisan exists. So we can use those. Or we could just match against the siteName if we want. If you take a look at siteName, you're going to get past exactly whatever comes before .test. So if you wanted to, you could just say return siteName Floga. And there you go. Only Floga will be served by this driver now. It's up to you. Now, what if you like this driver so much

It's up to you. Now, what if you like this driver so much that you want to share it between your computers or share it with the other members of your team? The good news is you can have these files come directly from Git. Here's a great example of a custom ValetDriver. And any files that are under slash dist, for example, projects that were created directly with Vue or Webpack, will work perfectly with this driver. So what is it? It's a README.

will work perfectly with this driver. So what is it? It's a README. It's a ValetDriver file. And it's instructions for how to get this file downloaded into your driver's directory. This one assumes it's your only driver. You can also just download this file directly yourself. It's up to you. If you're building a driver specific to one project, there's another way to write it and potentially share it.

Using a localValetDriver8:13

If you're building a driver specific to one project, there's another way to write it and potentially share it. So create that same custom driver, but instead of placing it in Valet's global settings folder, place it in the directory for your app and name it localValetDriver.php. So let's go test that out. I'm going to grab this file and we're going to delete it from our global directory. And instead, go over to the

and we're going to delete it from our global directory. And instead, go over to the Floga directory. And here we'll make a new file called localValetDriver.php. So let's paste the contents of that file here. But before we do anything else, we need to rename this class from flogavaletdriver to localValetDriver. And finally, your localValetDriver should be under the global namespace. So we get rid of our namespace. And now we should be able to go back and it should work just fine.

So we get rid of our namespace. And now we should be able to go back and it should work just fine. And look at that, still working, even though we've gotten rid of the global version of that. And instead, we're working now with the localValetDriver version of it. When Valet serves any app, it'll look for a file in the local directory with the name localValetDriver.php. And if it finds it, it'll use that as the driver. This is great because, first of all,

And if it finds it, it'll use that as the driver. This is great because, first of all, it makes a lot more sense to make site-specific tweaks in a file that is actually attached to that site, whereas those global drivers we were talking about before make more sense if it's something you do across more than one site. But it's also great because you can commit that driver directly to the repository. Then all your collaborators will instantly have access to it. And you'll have access to it on any machine you clone this repository down on as well.

And you'll have access to it on any machine you clone this repository down on as well. So that's how Valet drivers work and how you can create a custom driver either globally or for a specific site.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟