kenprogrammer's avatar

Requested Resource NOT Found (livewire/message)

Am trying to use livewire in an exsiting Laravel 7. When I type in the input or click submit button i get the following error:

"The requested resource /livewire/message/frontend.view-product was not found on this server."

See code snippets below:

Loading component (ProductsController)

    public function viewProduct($product_id)
    {

        return view('frontend.pages.view-product')->with('product_id',$product_id);
    }

Normal blade (Layout)

@extends('frontend.layouts.master')
@section('content')
@livewire('frontend.view-product', ['product_id' => $product_id])
@endsection

Component blade

<form>
                <input type="hidden" wire:model="product_id" value="{{ $product->product_id }}">
                <input type="hidden" wire:model="product_name" value="{{ $product->product_name }}">
                <input type="hidden" wire:model="price" value="{{ $product->price }}">
                <input type="text" wire:model="quantity" value="1">
                @error('quantity') <span class="text-danger">{{ $message }}</span>@enderror

                <button wire:click.prevent="addToCart()" class="btn btn-primary btn-md mr-1 mb-2">
                    Add To Cart
                </button>
                <button type="button" class="btn btn-primary btn-md mr-1 mb-2">Add To Wishlist</button>
            </form>
            <div wire:loading wire:target="addToCart">
                <img src="{{ asset('system/img/spinner.gif') }}" alt="Processing...">
            </div>

Component

<?php

namespace App\Http\Livewire\Frontend;

use Livewire\Component;
use App\Models\Product;
use DB;

class ViewProduct extends Component
{
    public $product_id,$product_name,$price,$quantity,$subtotal;

    protected $rules = [
        'quantity' => 'required|numeric'
    ];


    public function render()
    {
        $product=Product::where('product_id',$this->product_id)->first();
        $photos=DB::table('product_photos')->where('product_id',$this->product_id)->get();

        return view('livewire.frontend.view-product')->with('product',$product)
                                                     ->withPhotos($photos);
    }

    /*
    * Add to cart
    */
    public function addToCart()
    {
        $this->validate();

        $customer_id=Auth::guard('customer')->user()->id;
        $subtotal=$this->price * $this->quantity;

        $add_to_cart=ShoppingCartModel::create([
            "customer_id"=>"$customer_id",
            "product_id"=>$this->product_id,
            "unit_price"=>$this->price,
            "quantity"=>$this->quantity,
            "subtotal"=>"$subtotal"
        ]);

        session()->flash('success', $this->product_name.' successfully added to cart!');
    }
}

Am new to livewire. Any help on what's happening will be highly appreciated.

0 likes
21 replies
Snapey's avatar

When you view your site, do you see public in the URL?

Snapey's avatar

@ahmedmessi Start your own question please.... and fix your deployment so that public is the document root

Snapey's avatar

do you have your routes cached? php artisan route:clear

2 likes
kenprogrammer's avatar

Not working evening after clearing route cache. If I understand well live perfoms requests via POST to "livewire/message" . I don't what am missing. Below is JS snippet after inspecting sources:

 // Forward the query string for the ajax requests.
        fetch(
            `${window.livewire_app_url}/livewire/message/${payload.fingerprint.name}`,
            {
                method: 'POST',
                body: JSON.stringify(payload),
                // This enables "cookies".
                credentials: 'same-origin',
                headers: {
                    'Content-Type': 'application/json',
                    'Accept': 'text/html, application/xhtml+xml',
                    'X-CSRF-TOKEN': getCsrfToken(),
                    'X-Socket-ID': this.getSocketId(),
                    'X-Livewire': true,

                    // We'll set this explicitly to mitigate potential interference from ad-blockers/etc.
                    'Referer': window.location.href,
                },
            }
        )

It's failing at this line:

${window.livewire_app_url}/livewire/message/${payload.fingerprint.name
Snapey's avatar

run php artisan route:list and check the route is there as expected.

Also check app_url is correct for how you have the site hosted.

kenprogrammer's avatar

Currently running on localhost served by PHP development server.

The route is there (POST)

"livewire/message/{name} Livewire\Controllers\HttpConnectionHandler"

kenprogrammer's avatar

I've cleared config.cache,compiled views to no avail. browser console log:

"Failed to load resource: the server responded with a status of 404 (Not Found) :8080/livewire/message/frontend.view-product:1"

GeordieJackson's avatar

Instead of

<button wire:click.prevent="addToCart()"

You could try:

<form wire:click.prevent="addToCart()"

Only from memory, but I'm sure I had this problem and that's what it was. i.e. it was doing a submit when typing in the form fields.

Worth a try anyway. 👍

1 like
kenprogrammer's avatar

Actually I had tried this,but still the same error. As you mentioned,the form is being submitted when I start typing quantity. Also when I click the button it's the same issue.

<form wire:submit.prevent="addToCart">

Tried also

<form wire:submit.prevent="addToCart()">
GeordieJackson's avatar

Looking again, the error message is:

"The requested resource /livewire/message/frontend.view-product was not found on this server."

But it should be looking for:

"/livewire/message/frontend/view-product"

Note the slash instead of the dot in the view's path.

I suspect that the component's render() method is where something is going wrong. It's looking for a view called frontend.view-product rather than view-product in the frontend folder. Anyway, that might be the clue you need to dig deeper.

1 like
kenprogrammer's avatar

Ok thanks for the highlight. Though I changed to jQuery.

1 like
webcu's avatar
webcu
Best Answer
Level 2

Hi @kenprogrammer,

Are you using the local development server that Laravel provides php artisan serve or other? If other, you need to be sure of activate Apache's mod_rewrite or similar.

I was using the built-in server of PHP php -S localhost:80 -t public and I was having the same problem that you. I changed to Laravel local development server and the problem disappeared :) The file server.php is in charge of emulate Apache's mod_rewrite functionality.

Thanks to @geordiejackson for pointing out the issue with the view's path. That was what helped me to realize what was the problem.

1 like
kenprogrammer's avatar

Thanks. Am sure this could be the issue since am also using PHP built in server.

strats's avatar

Hi everyone! I also had this problem, I did a lot of things and experiments, more than 3 days hehe, and I see something: After publish the config file (config/livewire,php) I tested livewire on a route different of base public path (public/), for example I was running Livewire in:

"http:localhost/my-project/public/prefix/anotherprefix/routename"

And always gave me the error: 404 Not found, livewire/message/{name}, but when I ran the command: php artisan route:list, the route was here!

I tried a lot of things, change the folders name, I did 2 projects only for testing purposes, and also I tried to change the "livewire.js" file, but nothing works...

The only thing that works was... Set a base route, for example:

// routes/web.php

Route::get('/', function() {
	return view('livewire-base');
});

And the code of the view is:

<!DOCTYPE html>
<html lang="en">

<head>
    @livewireStyles

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    @livewireScripts
</body>

</html>

Yes!, a basic structure of HTML with the Livewire blade directives, And that's all!, Livewire already works on any route...

I thing this is a Livewire bug, I don't know, maybe try to find the scripts on the base path of project, in this case "public/", but it isn't here you will have the 404 error, on every change...

Also, I ran the command: php artisan serve And works very well, but I needed run this project under Apache server, because our remote server use it.

Let me know if it works for you or anybody, I know, this could be very frustrating hehe.

3 likes
georgeynr's avatar

@strats Thanks! You saved my day! I was getting a headache finding the solution. Searched everywhere. Finally found it here. Thanks for the solution.

Chris R.'s avatar

Thank You @strats! You save my day (or mayby week.)! I had the same issue - i deleted default route '/', because i didnt need it and i did it before i install livewire. I would never have thought that these things were related.

Please or to participate in this conversation.