Skip to content

Instantly share code, notes, and snippets.

@DykiSA
Last active September 19, 2023 12:11
Show Gist options
  • Save DykiSA/ff933600aa8e69941d4987ca8fa62c4f to your computer and use it in GitHub Desktop.
Save DykiSA/ff933600aa8e69941d4987ca8fa62c4f to your computer and use it in GitHub Desktop.
Setup Laravel 10 inside subfolder with Apache server, (and with Livewire 3)

Notable steps on installing Laravel 10 inside subfolder

First make sure you already create Laravel 10 project, see Laravel 10 Installation Guide.

This is useful to make your url accessible without navigating to /public folder.

  1. Create a .htaccess file and put it in your root folder of your project with the content below.

    # this file must be in the root directory of your project
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews -Indexes
        </IfModule>
    
        RewriteEngine On
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule ^ ^$1 [N]
    
        RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
        RewriteRule ^(.*)$ public/$1
    
        # Send Requests To Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    
  2. Create new file named index.php in root folder of your project and with the content below:

    <?php require_once('public/index.php');
  3. [OPTIONAL] Put LivewireController.php file to your app/Http/Controllers folder inside your project with the content below. Used in a case where for example, you are working with xampp and put the project inside the htdocs folder. Obviously you would access the site by the url http://localhost/chirper instead of http://localhost:<port>. Keep in mind, you need to change the $baseUrl value from chirper to your project path relative to the host.

    This is OPTIONAL step, if you are not supposed to use Livewire, you shouldn't need to use the LivewireController and skip this step.

    <?php
    // this file must be placed in the app/Http/Controllers	
    namespace App\Http\Controllers;	
    
    use Illuminate\Support\Facades\Route;	
    use Livewire\Livewire;	
    
    /**	
     * This controller is necessary to extend only if you are working in subdirectory of the root host.	
     * For example, you are working with xampp and put the project inside the htdocs folder.	
     * Obviously you would access the site by the url {@code http://localhost/chirper} instead of {@code http://localhost:<port>}.	
     * Otherwise you don't need to extend this controller.	
     */	
    class LivewireController extends Controller	
    {	
    
        /**	
         * @link https://livewire.laravel.com/docs/installation#configuring-livewires-update-endpoint	
         * @link https://livewire.laravel.com/docs/installation#customizing-the-asset-url	
         */	
        public function __construct()	
        {	
            // change the $baseUrl to your current sub directory	
            $baseUrl = '/chirper';	
            // set liveware asset route to look into custom base url	
            Livewire::setScriptRoute(function ($handle) use ($baseUrl) {	
                return Route::get($baseUrl . '/livewire/livewire.js', $handle);	
            });	
            // set liveware update route to look into custom base url	
            Livewire::setUpdateRoute(function ($handle) use ($baseUrl) {	
                return Route::post($baseUrl . '/livewire/update', $handle);	
            });	
        }	
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment