【问题标题】:How to configure Laravel 5.1 routing?如何配置 Laravel 5.1 路由?
【发布时间】:2015-11-07 05:42:50
【问题描述】:

我已经通过 composer 安装了一个新的 laravel 5.1。默认的预定义路由如下所示。

Route::get('/', function () {
    return view('welcome');
});

按照 Laravel 5.1 的文档安装和基本配置后,它可以正常使用上述路由。但是当我把它改成:

Route::get('test', function () {
    return view('welcome');
});   

or 

Route::get('/test', function () {
    return view('welcome');
});   

并尝试通过以下方式访问同一页面:

http://localhost/mylaravel/public/test

我遇到以下错误。

Not Found
The requested URL /mylaravel/public/test was not found on this server.

看起来好像路由不起作用。我已启用mod_rewrite,如下所示。

a2enmod rewrite

service apache2 restart

我尝试了三种不同的.htaccess,如下所示。

# the defualt one
<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>

# attempt two
Options +FollowSymLinks
RewriteEngine On

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

# attempt three
<IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule . /index.php [L]
</IfModule>

它根本不起作用。我忘了配置什么?我该如何解决这个路由问题?

【问题讨论】:

  • 你有本地服务器在运行吗?
  • 是的,当我通过localhost/mylaravel/public 访问该页面时,我可以看到欢迎页面。
  • 好的,正在检查。你的.htaccess 文件在哪里?
  • 试试这个Route::get('/test', 'welcome');
  • @Stuart Wagner:我的 .htaccess 默认位于公共目录中。

标签: php .htaccess laravel routing laravel-5


【解决方案1】:

在这些家伙的帮助下,我已经解决了这个问题,因为我做了以下事情。在命令行中。

sudo a2enmod rewrite

sudo service apache2 restart

sudo nano /etc/apache2/sites-available/000-default.conf

找到DocumentRoot /var/www/html并在下面直接添加以下行:

<Directory "/var/www/html">
    AllowOverride All
</Directory>

并使用以下命令重新启动 apache。

sudo service apache2 restart

使用 Laravel 安装提供的默认 .htaccess 即可。

【讨论】:

    【解决方案2】:

    问题在于您的本地服务器正在运行。 通过在控制台中运行以下命令来启动您的服务器:

    php artisan serve --host=localhost --port=8080
    

    然后尝试通过此地址访问您的应用:

    http://localhost:8080/test
    

    在 laravel 应用程序中,所有请求都由您公共目录中的 index.php 文件接收,并且当您请求类似
    http://localhost/mylaravel/public/test
    然后服务器认为您正在请求静态文件。

    【讨论】:

      猜你喜欢
      • 2015-10-12
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 2015-10-04
      • 2016-02-11
      • 1970-01-01
      相关资源
      最近更新 更多