Laravel PHP 框架的新手。“/”以外的路由不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18491760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:20:31  来源:igfitidea点击:

New to Laravel PHP framework. Routes other than "/" doesn't work

phplaravelhttp-status-code-404

提问by Rafael Adel

I'm a beginner at Lavarel framework. I know about MVC structure, Since I've used it before inside ASP.net, But using Laravel is quite confusing to me.

我是 Lavarel 框架的初学者。我知道 MVC 结构,因为我之前在 ASP.net 中使用过它,但是使用 Laravel 对我来说很困惑。

I've installed Laravel inside photozoomdirectory using:

我已经photozoom使用以下命令在目录中安装了 Laravel :

composer create-project laravel/laravel photozoom --prefer-dist

Here's my app/routes.php:

这是我的app/routes.php

<?php

Route::get('/', function()
{
    return View::make('hello');
});

Route::get('users', function()
{
    return 'users route is working!';
});

When i run http://localhost/photozoom/public/users, I get 404 Not Founderror.

当我运行时http://localhost/photozoom/public/users404 Not Found出现错误。

But when i try http://localhost/photozoom/public/, The route for /is invoked and the corresponding view is called.

但是当我尝试时http://localhost/photozoom/public//调用了路由并调用了相应的视图。

I even tried to create a view for the usersroute. Using Laravel documentation. I've created two files :

我什至试图为users路线创建一个视图。使用 Laravel 文档。我创建了两个文件:

layout.blade.php:

layout.blade.php

<html>
    <head>
        <title>Laravel Quickstart</title>
    </head>
    <body>
        <h1>Laravel Quickstart</h1>

        @yield('content')
    </body>
</html>

users.blade.php:

users.blade.php

@extends('layout')

@section('content')
    Users!!!
@stop

But still, When i call http://localhost/photozoom/public/usersI get 404 Not Found error.

但是,当我打电话时,http://localhost/photozoom/public/users我得到404 Not Found error.

Here's my public/.htaccessfile:

这是我的public/.htaccess文件:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

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

I'm using PHP 5.5, Apache 2.4.6 .

我使用的是 PHP 5.5, Apache 2.4.6 。

Any help would be appreciated.

任何帮助,将不胜感激。



SOLVEDAfter enabling mod_rewrite, I had to enable AllowOverridetoo.

已解决启用 mod_rewrite 后,我也必须启用AllowOverride

回答by Franz

Try http://localhost/photozoom/public/index.php/usersfor now. And then enable pretty URLs.

暂时试试http://localhost/photozoom/public/index.php/users。然后启用漂亮的 URL

回答by Z00tj3

The .htaccess file in the /public directory enables pretty URLs. In order for the .htaccess file to do its work:

/public 目录中的 .htaccess 文件启用了漂亮的 URL。为了让 .htaccess 文件完成它的工作:

  • Apache2 must have mod_rewrite enabled (a2enmod rewrite)
  • In your Apache configuration you must use the AllowOverride option to allow the .htaccess file to 'override' your default Apache2 configuration.
  • Apache2 必须启用 mod_rewrite (a2enmod rewrite)
  • 在您的 Apache 配置中,您必须使用 AllowOverride 选项来允许 .htaccess 文件“覆盖”您的默认 Apache2 配置。

For example:

例如:

<Directory /var/www/photozoom/> 
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

回答by Lahiru Abeyrathna

There are two AllowOverride in the httpd.conf file.

httpd.conf 文件中有两个 AllowOverride。

<Directory />
    AllowOverride All
    Require all denied
</Directory>

and

DocumentRoot "D:/www"
<Directory "D:/www">
    AllowOverride All
    Require all granted
</Directory>

回答by Dilip Suthar

if you're running windows 10 then you just need to do one thing simple as enough.

如果您运行的是 Windows 10,那么您只需要做一件足够简单的事情。

create new folder in c:/wamp64/www/ and copy your all file from laravel folder and paste in folder which you just created thats my solution.

在 c:/wamp64/www/ 中创建新文件夹并从 laravel 文件夹中复制所有文件并粘贴到您刚刚创建的文件夹中,这就是我的解决方案。