• xampp
  • Step-by-Step Guide to Run Laravel Spark in a Subdirectory

To run Laravel Spark in a subdirectory instead of the default ports (80 or 8000), you need to configure your Laravel application to work correctly under a subdirectory.


Step-by-Step Guide to Run Laravel Spark in a Subdirectory

1. Move Laravel Spark to a Subdirectory

Let's assume your Laravel Spark project is moved to a subdirectory, e.g., /opt/lampp/htdocs/myapp.

So, your site URL will be something like:

http://localhost/myapp/

2. Update the .env File

In your .env file, update the APP_URL to reflect the subdirectory:

APP_URL=http://localhost/myapp

If you're using HTTPS, update it accordingly:

APP_URL=https://example.com/myapp

3. Update Laravel Routes in routes/web.php

Modify all route definitions to ensure they respect the subdirectory. Instead of:

Route::get('/', function () {
    return view('welcome');
});

Use:

Route::prefix('myapp')->group(function () {
    Route::get('/', function () {
        return view('welcome');
    });

    // Other routes
    Route::get('/dashboard', 'DashboardController@index')->name('dashboard');
});

This ensures that all routes will now be accessible under:

http://localhost/myapp/
http://localhost/myapp/dashboard

4. Update the public Folder Path

If you are running Laravel Spark in a subdirectory, you need to configure public as follows:

Option 1: Modify .htaccess (Recommended)

In the public folder of Laravel, modify the .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /myapp/   # Add this line
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /myapp/index.php [L]   # Modify this line
</IfModule>

Option 2: Modify server.php

In the server.php file, update:

$_SERVER['SCRIPT_NAME'] = '/myapp/index.php';
$_SERVER['PHP_SELF'] = '/myapp/index.php';

5. Update Laravel's config/app.php

Open config/app.php and update:

'url' => env('APP_URL', 'http://localhost/myapp'),

6. Configure Apache for Subdirectory (If using Apache)

Modify your Apache Virtual Host Configuration file (usually in /etc/apache2/sites-available/000-default.conf for Ubuntu):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /opt/lampp/htdocs/myapp/public

    <Directory /opt/lampp/htdocs/myapp/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Then restart Apache:

sudo systemctl restart apache2

7. Configure Nginx for Subdirectory (If using Nginx)

Modify your Nginx configuration file (e.g., /etc/nginx/sites-available/default):

server {
    listen 80;
    server_name example.com;
    root /opt/lampp/htdocs/myapp/public;
    
    index index.php index.html index.htm index.nginx-debian.html;

    location /myapp {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Then restart Nginx:

sudo systemctl restart nginx

8. Clear Config & Cache

Run the following Artisan commands to clear the cache:

php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan optimize

Final Testing

Now, open your browser and go to:

http://localhost/myapp/

or if using a domain:

http://example.com/myapp/

If everything is set up correctly, Laravel Spark should now work inside the subdirectory.


Additional Fixes (If Required)

  • If CSS and JS are not loading properly, update your vite.config.js or mix.config.js:

      mix.setResourceRoot('/myapp/');

    Then run:

      npm run dev
  • If you face session issues, update:

      SESSION_DOMAIN=localhost

Let me know if you encounter any issues! 🚀

Running Laravel Spark in a subdirectory involves several steps, including configuring your web server and adjusting Laravel's routing. Here's a step-by-step guide to achieve this:

Step 1: Set Up Laravel Spark

First, ensure you have Laravel Spark installed in your Laravel project. If not, follow these steps:

  1. Create a new Laravel project:

       cd ~/Sites
       laravel new spark-blog-post
       cd spark-blog-post
  2. Install Laravel Spark:

       composer require laravel/spark
       php artisan spark:install
  3. Configure Spark:

    • Follow the prompts to set up Spark.
    • Edit your .env file to include your Stripe keys and other necessary configurations.

Step 2: Configure Web Server (Nginx or Apache)

To run Laravel in a subdirectory, you need to configure your web server to point to the correct directory.

For Nginx:

  1. Create a new Nginx configuration file (e.g., spark-subdir.conf) with the following content:

       server {
           listen 80;
           server_name example.com;
    
           # Point to the Laravel public directory
           location /spark {
               alias /path/to/your/project/public;
               try_files $uri $uri/ /spark/index.php?$query_string;
    
               location ~ \.php$ {
                   try_files $uri =404;
                   fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
                   fastcgi_param SCRIPT_FILENAME $request_filename;
                   include fastcgi_params;
               }
           }
    
           # Optional: Redirect root to subdirectory
           # location / {
           #     return 301 /spark;
           # }
       }
  2. Link the configuration file to Nginx's sites-enabled directory and reload Nginx.

For Apache:

  1. Create or modify an .htaccess file in your Laravel project's public directory:

       
           RewriteEngine on
           RewriteBase /spark/
    
           RewriteCond %{REQUEST_FILENAME} !-d
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteRule . index.php [L]
  2. Ensure Apache is configured to serve the Laravel public directory from the subdirectory.

Step 3: Adjust Laravel Routing

To ensure all routes are prefixed with the subdirectory path, modify the RouteServiceProvider.php:

// File: app/Providers/RouteServiceProvider.php

protected function mapWebRoutes()
{
    Route::middleware('web')
        ->prefix('spark') // Prefix all web routes with 'spark'
        ->namespace($this->namespace)
        ->group(base_path('routes/web.php'));
}

Step 4: Update Asset URLs

Modify the asset helper function in helpers.php to include the subdirectory path:

// File: vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
// Note: This is not recommended to edit directly; instead, create a custom helper.

function asset($path, $secure = null)
{
    return app('url')->asset("spark/public/".$path, $secure);
}

Alternatively, you can use the ASSET_URL environment variable to set a custom base URL for assets:

ASSET_URL=https://example.com/spark/public

Step 5: Test Your Setup

  1. Access your application at http://example.com/spark.
  2. Verify routes are working correctly by accessing routes like http://example.com/spark/login.

Additional Considerations

  • Security: Ensure sensitive files like .env are not accessible from the web by keeping them outside the public directory.
  • Subdomain: Consider using a subdomain instead of a subdirectory if possible, as it simplifies configuration and security.

By following these steps, you should be able to run Laravel Spark in a subdirectory. However, keep in mind that Laravel is designed to be served from the root of a web directory for security reasons.

Citations:
[1] https://www.youtube.com/watch?v=tZGg2kdOjhQ
[2] https://mattstauffer.com/blog/introducing-laravel-spark-a-deep-dive/
[3] https://stackoverflow.com/questions/58856071/laravel-application-deployed-in-sub-directory-all-routes-broken
[4] https://laravel.io/forum/11-25-2014-installing-laravel-in-a-subdirectory
[5] https://github.com/laravel/framework/issues/51210
[6] https://stackoverflow.com/questions/50151301/deploy-laravel-project-on-subfolder/50151540
[7] https://stackoverflow.com/questions/64530889/laravel-make-routing-to-subfolder-on-deploy
[8] https://serverfault.com/questions/771585/how-can-i-get-laravel-app-routing-to-work-in-a-sub-folder-of-a-wordpress-site
[9] https://laracasts.com/discuss/channels/laravel/laravel-4-setting-base-url-to-subfolder
[10] https://stackoverflow.com/questions/74529806/deploy-laravel-project-on-subfolder-in-subdirectory
[11] https://laravel.io/forum/07-09-2015-deploy-laravel-5-on-sub-directory-of-shared-hosting
[12] https://min.io/docs/minio/linux/integrations/setup-nginx-proxy-with-minio.html
[13] https://laracasts.com/discuss/channels/spark/some-questions-about-laravel-spark
[14] https://laracasts.com/discuss/channels/servers/deploy-laravel-in-subdirectory
[15] https://laracasts.com/discuss/channels/spark/overwritereplace-spark-routes
[16] https://laracasts.com/discuss/channels/spark/spark-local-development-wrong-path-in-js-404-error
[17] https://laracasts.com/discuss/channels/laravel/how-to-deploy-laravel-11-in-subfolder-on-server
[18] https://installatron.com/codeigniter?hl=en_GB&src=typd&subject=Do+Not+Sell+My+Personal+Information&index=32&playerColor=688AAD&list=UUCTOowMwbeih06bI9daQmPA&version=v1&videoHeight=360&videoWidth=640&q=%5B+++HackerSite%3A+Kunghac.com++%5D%2Cgoogle+hacking+database+whatsapp%2Cfira+follower+hack%2Cthe+gram+hacke+com%2Cwhatsapp+dp+hack%2Cthe+gramhacker%2Cwhatsapphacked%2Cfree+whatsapp+followers+hack+no+human+verification%2Cwhatsapp+followers+hack%2Ckali+linux+whatsapp+hacking%2Chack+reels+views%2C
[19] https://stackoverflow.com/questions/30497575/install-laravel-5-app-in-subdirectory-with-htaccess/30497841
[20] https://laravel.com/docs/11.x/configuration
[21] https://laracasts.com/discuss/channels/laravel/add-sub-domain-to-existing-routes-ignition-spark
[22] https://laracasts.com/discuss/channels/spark/customising-links-on-spark-billing-page
[23] https://joebubna.github.io/Cora/documentation/v2/collections/
[24] https://laracasts.com/discuss/channels/spark/is-making-changes-to-files-inside-the-spark-directory-ok
[25] https://stackoverflow.com/q/15938325
[26] https://javorszky.co.uk/2017/03/18/i-modified-the-token-authentication-in-laravel-spark/
[27] https://forum.codeigniter.com/showthread.php?pid=416438
[28] https://kirschbaumdevelopment.com/insights/custom-laravel-packages
[29] https://laravel.io/forum?filter=recent&page=96
[30] https://stackoverflow.com/questions/33127507/how-to-set-base-url-to-subfolder-in-laravel-4-2
[31] https://github.com/livewire/livewire/discussions/4750
[32] https://github.com/laravel/horizon/issues/660
[33] https://laravel.com/docs/11.x/telescope
[34] https://github.com/laravel/framework/issues/32236


Answer from Perplexity: pplx.ai/share