【问题标题】:Laravel 8.53 - Routing doesnt work on any of pagesLaravel 8.53 - 路由不适用于任何页面
【发布时间】:2021-10-14 22:25:11
【问题描述】:

解决方案

感谢 @Gert B.

找到了解决方案。

只需将任何虚拟主机添加到您的 Laravel 应用程序(我为我添加的) laravel1.test

如何添加虚拟主机: 转到 C:\Windows\System32\drivers\etc\hosts

添加行:

127.0.0.1 laravel1.test (or your virtual host name)

并将其添加到 C:\xampp\apache\conf\extra httpd-vhosts 中的虚拟主机(如果使用 xampp)

<VirtualHost *:80>
        ServerName www.laravel1.test
        ServerAlias laravel1.test
        DocumentRoot "C:\xampp\htdocs\Laravel1\public"
        
        <Directory "C:\xampp\htdocs\Laravel1\public">
            # use mod_rewrite for pretty URL support
           RewriteEngine on
            # If a directory or a file exists, use the request directly
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            # Otherwise forward the request to index.php
            RewriteRule . index.php

            # use index.php as index file
            DirectoryIndex index.php
            # ...other settings...
            # Apache 2.4
            Require all granted
           
            ## Apache 2.2
            # Order allow,deny
            # Allow from all
       </Directory>
   </VirtualHost>

我无法通过 Laravel 8.5 框架中的路由问题。我知道到目前为止已经有很多关于这个问题的请求,但是任何给定的解决方案对我的情况都没有帮助。我将展示前端代码,因为后端是一样的,也不起作用。现在,唯一有效的是 index.php。

感谢最新的 Laravel,我知道所有错误都隐藏在简单的错误 404 下。

Bug image

我已经试过了:

  • 更改 apache/conf/httpd 解决方案 AllowOverride All
  • 清除整个缓存和路由缓存
  • 代码中任何可能的拼写错误

仍然没有任何可用的 anwser。

我的前端控制器在 app/Http/Controllers


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FrontendController extends Controller
{
    public function index()
    {
        return view('frontend.index');
    }

    public function object()
    {
        return view('frontend.object');
    }

    public function article()
    {
        return view('frontend.article');
    }


    public function person()
    {
        return view('frontend.person');
    }

    public function room()
    {
        return view('frontend.room');
    }

    public function roomSearch()
    {
        return view('frontend.roomsearch');
    }

}

我在 app/routes 中的 web.php


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FrontendController;


##Frontend routes

Route::get('/','FrontendController@index')->name('index');
Route::get('/object','FrontendController@object')->name('object');
Route::get('/article','FrontendController@article')->name('article');
Route::get('/person','FrontendController@person')->name('person');
Route::get('/room','FrontendController@room')->name('room');
Route::get('/roomSearch','FrontendController@roomsearch')->name('roomSearch');


##Backend routes
Route::group(['prefix'=>'admin'],function(){

    Route::get('/','BackendController@index')->name('adminHome');

    Route::get('/cities','BackendController@cities')->name('cities');
    Route::get('/myObjects','BackendController@myobjects')->name('myObjects');
    Route::get('/profile','BackendController@profile')->name('profile');
    Route::get('/saveObject','BackendController@saveobject')->name('saveObject');
    Route::get('/saveRoom','BackendController@saveroom')->name('saveRoom');

});

.htacces 文件在 app/public 中

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
       Options -MultiViews
   </IfModule>
   Options +FollowSymlinks
   RewriteEngine On


   # Redirect Trailing Slashes...
   RewriteRule ^(.*)/$ /$1 [L,R=301]

   # Handle Front Controller...
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^ index.php [L]
</IfModule>

我的路线 image

【问题讨论】:

  • 你的域名应该指向 laravel 公用文件夹。现在路由失败了,因为/laravel1/public 在网址中。大多数情况下,每个项目都会创建一个本地域
  • @GertB。感谢您的回答,对于这个问题有什么好的解决方案?我应该更改 htacces 中的某些内容吗?
  • 这取决于您使用的本地机器。您可以为域添加一个虚拟主机(如 localhost ),您可以使用 laravel 的服务,我使用 Valet 作为我的开发环境...
  • 天啊。它确实奏效了。我只需要添加虚拟主机就可以了……我不敢相信。先生,感谢您的时间和解决方案!
  • 不客气!

标签: php laravel


【解决方案1】:

解决方案

感谢 @Gert B.

找到了解决方案。

只需将任何虚拟主机(我为我添加的)laravel1.test 添加到您的 Laravel 应用程序中

如何添加虚拟主机:转到 C:\Windows\System32\drivers\etc\hosts

添加行:

127.0.0.1 laravel1.test (or your virtual host name)

并将其添加到 C:\xampp\apache\conf\extra httpd-vhosts 中的虚拟主机(如果使用 xampp)

<VirtualHost *:80>
        ServerName www.laravel1.test
        ServerAlias laravel1.test
        DocumentRoot "C:\xampp\htdocs\Laravel1\public"
        
        <Directory "C:\xampp\htdocs\Laravel1\public">
            # use mod_rewrite for pretty URL support
           RewriteEngine on
            # If a directory or a file exists, use the request directly
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            # Otherwise forward the request to index.php
            RewriteRule . index.php

            # use index.php as index file
            DirectoryIndex index.php
            # ...other settings...
            # Apache 2.4
            Require all granted
           
            ## Apache 2.2
            # Order allow,deny
            # Allow from all
       </Directory>
   </VirtualHost>

【讨论】:

    猜你喜欢
    • 2018-01-04
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2015-02-05
    • 2019-04-24
    • 2020-05-15
    • 1970-01-01
    相关资源
    最近更新 更多