laravel 5 只有根路由工作

2022-08-30 22:00:55

我是拉拉维尔的新手。我使用的是 Ubuntu 15.04。我使用作曲家安装了Laravel Framework版本5.1.7(LTS)和使用命令的灯服务器(我没有安装Homestead)。我正在使用IDE。$ sudo apt-get install lamp-server^PhpStorm 8.0.3

我创建了三条路线和一个控制器。该文件如下所示:PagesController.php

class PagesController extends Controller
{
    public function index()
    {
        return 'Welcome to my homepage!';
    }

    public function about()
    {
        return 'Learn a little about me.';
    }

    public function hello()
    {
        return 'Hello World!';
    }
}

和看起来像这样:routes.php

Route::get('/', 'PagesController@index');

Route::get('about', 'PagesController@about');

Route::get('hello', 'PagesController@hello');

每当我去(或)它工作时,它都可以正常工作,并向我显示消息。但每当我去或,我得到的是消息。http://localhost:63342/my-first-app/public/http://localhost:63342/my-first-app/public/index.phpWelcome to my homepage!http://localhost:63342/my-first-app/public/hellohttp://localhost:63342/my-first-app/public/about404 Not Found

此外,位于 的文件如下所示:.htaccessmy-first-app/public

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

我尝试过:

  • 我试过,或者它也不起作用。http://localhost:63342/my-first-app/public/index.php/hellohttp://localhost:63342/my-first-app/public/index.php/about
  • 我输入了命令,然后是命令,但它也不起作用。sudo a2enmod rewritesudo service apache2 restart
  • 我试过了,但它也不起作用。composer dump-autoload
  • 我从 换到 .现在它的一部分看起来像这样:AllowOverrideNoneAllapache2.conf

    <Directory />
        Options FollowSymLinks
        AllowOverride All
        Require all denied
    </Directory>
    
    <Directory /usr/share>
        AllowOverride All
        Require all granted
    </Directory>
    
    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    <Directory /srv/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    

    但它也不能解决问题。

更新 (7/15/2015):

运行的结果如下所示:php artisan route:list

+--------+----------+-------+------+--------------------------------------------+------------+
| Domain | Method   | URI   | Name | Action                                     | Middleware |
+--------+----------+-------+------+--------------------------------------------+------------+
|        | GET|HEAD | /     |      | App\Http\Controllers\PagesController@index |            |
|        | GET|HEAD | about |      | App\Http\Controllers\PagesController@about |            |
|        | GET|HEAD | hello |      | App\Http\Controllers\PagesController@hello |            |
+--------+----------+-------+------+--------------------------------------------+------------+

答案 1

启用 apache2 重写模块:

sudo a2enmod rewrite

然后重新启动 apache2 服务器:

sudo service apache2 restart

并确保到你的apache2配置文件。AllowOverride All


答案 2

很长一段时间以来,我一直遇到这个烦人的问题。在公用文件夹中打开文件并替换以下代码,备份原始代码以防万一。.htaccess

<IfModule mod_rewrite.c>

    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/index.php [L]

</IfModule>

推荐