Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant get a setup to work with Laravel Herd on Windows (even with a custom driver) #36

Open
kyloknight opened this issue Jun 13, 2024 · 4 comments

Comments

@kyloknight
Copy link

kyloknight commented Jun 13, 2024

I am using Laravel Herd on Windows 11 Pro to host all my local sites. Laravel, default WordPress as well as Themosis Framework installs work fine. Its only Lumberjack that I cant seem to configure correctly. I think this will be useful for a lot of users, as Laravel Herd is growing in popularity (everyone at our company uses it, as a lot of developers even on codeable.io).

I tried creating a custom Valet driver:

<?php 

namespace Valet\Drivers\Custom;

use Valet\Drivers\LaravelValetDriver;

class LumberjackValetDriver extends LaravelValetDriver
{
    public function serves(string $sitePath, string $siteName, string $uri): bool
    {
        return file_exists($sitePath.'/web/wp/wp-load.php');
    }

    public function isStaticFile(string $sitePath, string $siteName, string $uri)
    {
        // Define potential static file paths
        $staticFilePaths = [
            $sitePath.'/web'.$uri,
            $sitePath.'/web/app'.$uri,
            $sitePath.'/web/app/themes'.$uri,
            $sitePath.'/web/app/uploads'.$uri
        ];

        // Check if the static file exists in any of the defined paths
        foreach ($staticFilePaths as $staticFilePath) {
            if ($this->isActualFile($staticFilePath)) {
                return $staticFilePath;
            }
        }

        return false;
    }

    public function frontControllerPath(string $sitePath, string $siteName, string $uri): string
    {
        $_SERVER['PHP_SELF'] = $uri;
        $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];

        // Check if the request is for a wp-admin page or a static file
        if (strpos($uri, '/wp/') === 0 || $this->isStaticFile($sitePath, $siteName, $uri)) {
            if (is_dir($sitePath.'/web'.$uri)) {
                $uri = $this->forceTrailingSlash($uri);
                return $sitePath.'/web'.$uri.'/index.php';
            }
            return $sitePath.'/web'.$uri;
        }

        // Default to the main index.php for other requests
        return $sitePath.'/web/index.php';
    }

    private function forceTrailingSlash(string $uri): string
    {
        if (substr($uri, -1 * strlen('/wp/wp-admin')) == '/wp/wp-admin') {
            header('Location: '.$uri.'/');
            die;
        }

        return $uri;
    }
}

/wp/wp-admin works just fine. I can access the dashboard and all admin functionality without any issues. The problem is the front-end, images, js, css etc is never loaded (seems not static files load).

Any help would be appreciated, as we have quite a few sites using Lumberjack, and if we cant get this working we have to move them all over to Themosis Framework which would cost a lot of time, and not be ideal, as they are all working really great using lumberjack.

I did not put down version information for PHP or Lumberjack, as its not relevant to the issue.

@adamtomat
Copy link
Member

We tend to use Docker so I've not used Heard yet, but back when we used Laravel Valet we had to use this Valet Driver for Bedrock/Trellis. May be useful?

https://github.com/danielroe/trellis-valet-driver/blob/master/TrellisValetDriver.php

@kyloknight
Copy link
Author

@adamtomat, thank for such a quick response, really appreciated! I am just going to try a bit longer to figure it out. I adapted what you sent and it looks like this:

<?php

namespace Valet\Drivers\Custom;

use Valet\Drivers\LaravelValetDriver;

class LumberjackValetDriver extends LaravelValetDriver
{
    /**
     * Determine if the driver serves the request.
     *
     * @param string $sitePath
     * @param string $siteName
     * @param string $uri
     * @return bool
     */
    public function serves(string $sitePath, string $siteName, string $uri): bool
    {
        return file_exists($sitePath . '/web/app/mu-plugins/bedrock-autoloader.php') ||
            (is_dir($sitePath . '/web/app/') &&
                file_exists($sitePath . '/web/wp-config.php') &&
                file_exists($sitePath . '/config/application.php'));
    }

    /**
     * Determine if the incoming request is for a static file.
     *
     * @param string $sitePath
     * @param string $siteName
     * @param string $uri
     * @return string|false
     */
    public function isStaticFile(string $sitePath, string $siteName, string $uri)
    {
        $staticFilePath = $sitePath . '/web' . $uri;
        if ($this->isActualFile($staticFilePath)) {
            return $staticFilePath;
        }
        return false;
    }

    /**
     * Get the fully resolved path to the application's front controller.
     *
     * @param string $sitePath
     * @param string $siteName
     * @param string $uri
     * @return string
     */
    public function frontControllerPath(string $sitePath, string $siteName, string $uri): string
    {
        $_SERVER['PHP_SELF'] = $uri;
        $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];

        if (strpos($uri, '/wp/') === 0) {
            return is_dir($sitePath . '/web' . $uri)
                ? $sitePath . '/web' . $this->forceTrailingSlash($uri) . '/index.php'
                : $sitePath . '/web' . $uri;
        }
        return $sitePath . '/web/index.php';
    }

    /**
     * Redirect to uri with trailing slash.
     *
     * @param string $uri
     * @return string
     */
    private function forceTrailingSlash(string $uri): string
    {
        if (substr($uri, -1 * strlen('/wp/wp-admin')) == '/wp/wp-admin') {
            header('Location: ' . $uri . '/');
            die;
        }
        return $uri;
    }
}

wp admin is fine still as before, but front facing pages still don't fetch any assets for some odd reason. I just dont have the time to go through all of lumberjacks core code to figure this out. I will have to work on a staging server for now rather than local. Thanks again for that super quick response <3

@adamtomat
Copy link
Member

adamtomat commented Jun 13, 2024

My gut feel is that this is less of a specific Lumberjack issue and more about Heard + Bedrock. Lumberjack is just the theme, so I'd be surprised if anything LJ related is causing issues especially as we don't do anything to asset loading etc.

I'd recommend seeing if there are any other folks experiencing this issue with Heard/Valet and Bedrock/Trellis as that directory structure is common regardless of the theme being used.

Also if this is blocking you, you can also spin up a local PHP server, e.g:

php -S 127.0.0.1:8080

This uses the installed PHP version on your machine instead of Heard, but may be useful - I've defo done this a bunch in the past 😅

@kyloknight
Copy link
Author

Thanks @adamtomat. When I figure it out I will come back and post it here for other users :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants