skip to Main Content

I had a problem regarding how to use laravel livewire since I have my own reason for not using PHP artisan serve, Nginx, vhosts, or any PHP serve. Just normal access using .htaccess to project like normal raw PHP (only for development stage).

First my setup for laravel,
I am using Laravel 8 and the way I use to access the laravel project without PHP artisan serve is by using .htaccess which is like this:

<IfModule !mod_rewrite.c>
   deny from all
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_URI} /$
RewriteRule ^ %{REQUEST_URI}index.php [L,R=301]

RewriteCond %{REQUEST_URI} $
RewriteRule ^(.*)$ public/$1 [L,QSA]

<Files .env>
    Order allow,deny
    deny from all
</Files>
</IfModule>

With this, I am able to access my project and the URL looks like this

http://localhost/Laravel/v8/suhv2livewire/public/index.php/login

All is working well until I install livewire next. The installation is good and the livewire is detected. But when I try to submit a form then it gives me either these 3 errors

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

From what I understand these errors mean, it returns HTML instead of JSON which should be 404 in my case (just my assumption)

404 Not Found

The GET method is not supported for this route. Supported methods: POST.

By using PHP artisan serve, it works without any problem and zero configuration. However, without PHP artisan service, it becomes a problem and a headache for me. I read the documentation regarding asset_url and try to change that such as:

'asset_url' => 'http://localhost/Laravel/v8/suhv2livewire'

yet still not working and get either 3 errors I show above.

I found a similar question on StackOverflow but not working and others said about Nginx and so on. I don’t want that.

Please help if you guys found any solution. It would be better if I can use livewire like any other javascript file like

require('livewire')

like alpinejs.

What annoys me is that I don’t know either my .htaccess, livewire asset_url, or any other section is the real problem.

Thanks for your help.

2

Answers


  1. in config/livewire.php

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

    then in livewire asset will be depends on .env in APP_URL

    so you need to configure your url in .env

    APP_URL=http://localhost/Laravel/v8/suhv2livewire/public
    

    NOTE : this change will effect once you run php artisan config:clear

    Login or Signup to reply.
  2. try running:

    php artisan livewire:publish
    

    then

    • open config/livewire.php
    • change ‘asset_url’ => null to ‘asset_url’ => ‘http://localhost/LaravelProject/public’
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search