繁体   English   中英

如何配置Laravel 5.1路由?

[英]How to configure Laravel 5.1 routing?

我已经通过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>

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

在这些人的评论的帮助下,我按照以下步骤解决了此问题。 在命令行中。

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 ,它可以工作。

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

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

然后尝试通过以下地址访问您的应用程序:

http://localhost:8080/test

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

暂无
暂无

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

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