【问题标题】:Invalid route action: [resources\views\livewire\product-index.blade.php]无效的路由操作:[resources\views\livewire\product-index.blade.php]
【发布时间】:2021-09-08 14:02:27
【问题描述】:

我已经尝试为我的产品制作视图,

product-index.blade.php

<div class="container">
<div class="row">
    <div class="col">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="{{ route('home') }}" class="text-dark">Home</a></li>
              <li class="breadcrumb-item active" aria-current="page">Product</li>
            </ol>
          </nav>
    </div>
</div>
<h2><strong>Art</strong>hestic</h2>

navbar.blade.php

<li class="nav-item">
                    <a class="nav-link" href="{{ route('products') }}"  role="button"  aria-haspopup="true" aria-expanded="false">
                      Products
                    </a> 

web.php (当我使用此路由时,它会显示错误消息“无效路由操作:[resources\views\livewire\product-index.blade.php]。

Route::get('/products', 'resources\views\livewire\product-index.blade.php')->middleware('auth')->name('products');

但是当我使用这个时

Route::get('/products', App\Http\Livewire\ProductIndex::class)->middleware('auth')->name('products');

it doesn't show anything, if there's no error it would show the text from my product-index.blade

【问题讨论】:

  • 请分享更多详细信息,例如文本形式的错误消息,以及您解决错误的尝试
  • 另外,请编辑您的问题以包含 预期 行为。在 cmets 中,您提到“我的产品不会出现”,这是什么意思?
  • @NicoHaase 我已经编辑过了
  • 你能分享更多细节吗,比如App\Http\Livewire\ProductIndex的内容?另外,您是否尝试在Route::get 中使用正确的路由操作并且不是 模板名称?
  • 你的身体有{{ $slot }}吗? laravel-livewire.com/docs/2.x/…

标签: php laravel laravel-routing laravel-livewire


【解决方案1】:

大多数basic Laravel 路由接受一个 URI 和一个闭包作为第二个参数。注意不需要从resources/views 开始路径,因为 Laravel 已经知道你的视图在哪里。

routes/web.php

Route::get('/products', function () {
    return view('livewire.product-index'); 
});

或通过控制器

use App\Http\Livewire\ProductIndex;

Route::get('/products', [ProductIndex::class, 'index']);
// where 'index' is method name inside App\Http\Livewire\ProductIndex class. Name could be any and it should return view.

ProductIndex.php

class ProductIndex
{
  public function index() {
    return view('livewire.product-index');
  }
}

如果您需要将视图作为第二个参数传递,请将get 方法更改为view,就像here 所说的那样

Route::view('/products', 'livewire.product-index');

【讨论】:

    【解决方案2】:

    您正在通过您的路线中的一个视图。这应该是一个函数。

    Route::get('/products', function () {
        return view('product-index');
    });
    

    如果您的视图需要数据库中的数据,您应该在控制器中创建一个函数来获取数据并将其与视图一起返回。然后在你的路由文件中引用它,如下所示。

    Route::get('/producs', 'App\Http\Controllers\CONTROLLERNAME@FUNCTIONAME');
    

    【讨论】:

    • 我试图像这样use Illuminate\Support\Facades\Route; Route::get('/products', App\Http\Livewire\ProductIndex::class)-&gt;middleware('auth')-&gt;name('products'); 修复我的路线,但我的产品不会显示这是我的产品-index.blade &lt;/div&gt; &lt;h2&gt;&lt;strong&gt;Art&lt;/strong&gt;hestic&lt;/h2&gt; &lt;/div&gt;
    • @rpm192 您的回答在技术上是正确的,但他应该能够将他的 Livewire 组件与路由连接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    相关资源
    最近更新 更多