简体   繁体   English

Laravel 404页面未找到,路由存在

[英]Laravel 404 page not found, route exists

I am trying to navigate to my "productdetail" page but it givdes me a 404. The route to productdetail does exists.我正在尝试导航到我的“productdetail”页面,但它给了我一个 404。productdetail 的路径确实存在。 I am trying to give product information from shop to productdetail我正在尝试将产品信息从商店提供给 productdetail

My controller:我的 controller:

<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use App\Product;

class ProductsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function shopindex()
    {
        $productsOTs = DB::select(DB::raw("SELECT * FROM wiz.productimages WHERE Afkorting = 'PPI' LIMIT 83, 3"));
        return view('shop', compact('productsOTs'));
    }

    public function productdetail(Product $Product)
    {   
        return view('Products.productdetail', compact('productsOT'));

    }

}

My shop page link to productdetail:我的商店页面链接到产品详细信息:

           @foreach ($productsOTs as $productsOT)
                <div class="card ot-product" id="heightwidthfix">
                    <img class="card-img-top cardstop" src="{{$productsOT->imagelink}}" alt="Card image cap" id="myshopmodal1" height="400px" width="300px">
                    <div class="card-body">
                        <h5 class="card-title">{{$productsOT->Productomschrijving}}</h5>
                        <p class="card-text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
                    </div>
                    <div class="card-body">
                        <a href="/shop/productdetail/{{ $productsOT->Productcode }}" class="card-link">Bekijk hier het product</a>
                    </div>
                </div>
            @endforeach

My routes:我的路线:

Route::get('/shop', ['middleware' => 'auth', 'uses' => 'ProductsController@shopindex']);
Route::get('/shop/productdetail/{product}', ['middleware' => 'auth', 'uses' => 'ProductsController@productdetail']);

I have been struggling with this problem for a while now, i hope someone can help me.我已经为这个问题苦苦挣扎了一段时间,希望有人能帮助我。

Please run this commend请运行此推荐

php artisan route:cache php工匠路线:缓存

and also并且

php artisan cache:clear php工匠缓存:清除

Why don't you try to put a name for your route你为什么不试着为你的路线命名

Route::get('/shop/productdetail/{product}', ['middleware' => 'auth', 'uses' => 'ProductsController@productdetail'])->name('show-product');

then call it through it's name:然后通过它的名字来调用它:

<a href="{{route('show-product',$productsOT->Productcode) }}" class="card-link">Bekijk hier het product</a>

This will might solve your problem otherwise the problem is not on the route.这可能会解决您的问题,否则问题不在路线上。

Please use the following way请使用以下方式

Route::middleware(['auth'])->get('/shop/productdetail/{product}', 'ProductsController@productdetail');

I usually prefer to group the middleware so as to overcome writing it again and again by the following way我通常更喜欢将中间件分组,以克服通过以下方式一次又一次地编写它

Route::group(['middleware' => 'auth'], function () {
        /** Dashboard */
        Route::match(['get', 'post'], '/dashboard', 'AdminController@dashboard');
        /**Customer */
        Route::get('/customers', 'AdminController@customerListing');
});

Change this line:改变这一行:

<a href="/shop/productdetail/{{ $productsOT->Productcode }}" class="card-link">Bekijk hier het product</a>

into进入

<a href="{{ url('shop/productdetail/'.$productsOT->Productcode) }}" class="card-link">Bekijk hier het product</a>

This is the minor mistake.这是小错误。 Change this, I hope it is helpful.改一下,希望对你有帮助。 Thanks谢谢

我认为您传递的路由参数的问题是不合适的传递

<a href="/shop/productdetail/{{ $productsOT->relevant_id_colunm_name }}" class="card-link">Bekijk hier het product</a>

I had issue with .htaccess.我遇到了 .htaccess 的问题。 I uploaded, but it was somehow missing!我上传了,但不知何故丢失了! I faced some other problems, everything is documented with my .htaccess file content here: https://stackoverflow.com/a/64828062/1938507我遇到了其他一些问题,所有内容都记录在我的 .htaccess 文件内容中: https : //stackoverflow.com/a/64828062/1938507

Just type on your terminal只需在您的终端上输入

php artisan route:clear php工匠路线:清除

Hey buddy first thing to do this go to your command嘿,伙计,做这件事的第一件事就是听你指挥

  • php artisan route:clear
  • php artisan optimize

but I found something, I got worry in your code.但我发现了一些东西,我担心你的代码。

I prefer you 2 choices to solve this one我更喜欢你 2 个选择来解决这个问题

  • URL('/shop/productdetail/'.{{$id_here}})
  • or add router name in your routes.或在您的路线中添加路由器名称。 ->name('shop_productdetails); then use this in your blade {{route('shop_productdetails',$id)}}然后在你的刀片中使用它{{route('shop_productdetails',$id)}}

You're Welcome buddy不客气,伙计

why are u doing $productsOt->ProductCode instead of $productsOt->id ?你为什么要做$productsOt->ProductCode而不是$productsOt->id If you need to get using the ProductCode .如果您需要使用ProductCode Use利用

Route::get('/shop/productdetail/{product:ProductCode}', ['middleware' => 'auth', 'uses' => 'ProductsController@productdetail']);

if your column name is ProductCode如果您的列名是 ProductCode

Try following尝试关注

<a href="url('/shop/productdetail', [$productsOT->Productcode])" class="card-link">Bekijk hier het product</a>

If this does not work, try如果这不起作用,请尝试

php artisan route:clear

If that does not work, try如果这不起作用,请尝试

php artisan optimize

Solution 1解决方案 1

You are using the Route Binding feature but, not totally valid because the route segment and function parameter must be identical and this does not apply to what you did where your route segment is product and function parameter is Product , and this is invalid approach and when you pass the product id as a parameter, Laravel searches in the products table with the given id, and because the given id does not exist in the database Laravel trigger this page So, your code must be like so您正在使用Route Binding功能,但并不完全有效,因为路由段和 function 参数必须相同,这不适用于您在路由段为product且 function 参数为Product时所做的操作,这是无效的方法,何时您将产品ID作为参数传递, Laravel 使用给定的ID在products表中搜索,并且因为给定的ID在数据库中不存在 Laravel 触发此页面所以,您的代码必须像这样

web.php web.php

Route::get('/shop/productdetail/{product}', ['middleware' => 'auth', 'uses' => 'ProductsController@productdetail']);

ProductsController.php ProductsController.php

public function productdetail($product)
{   
    $products = \App\Models\Product::where("id", $product)->get();

    return view('Products.productdetail', compact('products'));
}

you may want to search with another column name than id so you must specify the other column name that you wish to search with as Laravel says here您可能想使用id以外的其他列名进行搜索,因此您必须指定要搜索的其他列名,如Laravel所说

Solution 2方案二

If you register these routes in a new route file other than web.php , you must define this file in RouteServiceProvider.php as Laravel says here如果您在web.php以外的新路由文件中注册这些路由,则必须在RouteServiceProvider.php中定义文件,如 Laravel 所说

The problem is a little bit tricky:问题有点棘手:

What is happening:怎么了:

Your Laravel application is currently using an out-dated routes cached file located in app/bootstrap/routes-x.php file instead of your routes files located in app/routes directory.您的 Laravel 应用程序当前正在使用位于 app/bootstrap/routes-x.php 文件中的过时路由缓存文件,而不是位于 app/routes 目录中的路由文件。

The solution:解决方案:

  1. manualy delete app/bootstrap/routes-x.php file.手动删除 app/bootstrap/routes-x.php 文件。

  2. in your app/routes (api.php or console.php... ) routes files replace any route that is using a closure with controllerName@functionName在你的应用程序/路由中(api.php 或 console.php...)路由文件替换任何使用 controllerName@functionName 闭包的路由

    example of route using a closure:使用闭包的路由示例:

    Route::get('/route-using-closure', function() { return 'Hello World'; }); Route::get('/route-using-closure', function() { return 'Hello World'; });

    Replace by:替换为:

    Route::get('/route-using-closure', 'controllerName@functionName'); Route::get('/route-using-closure', 'controllerName@functionName');

  3. Only run php artisan route:cache in dev mode or any other caching command to avoid this errors.仅在开发模式下运行 php artisan route:cache 或任何其他缓存命令以避免此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM