问题描述
我是laravel的新手.我正在使用Ubuntu 15.04.我使用作曲家安装了Laravel Framework版本5.1.7(LTS),并使用$ sudo apt-get install lamp-server^
命令安装了一个灯服务器(我没有安装Homestead).我正在使用PhpStorm 8.0.3
作为IDE.
I am new to laravel. I am using Ubuntu 15.04. I installed Laravel Framework version 5.1.7 (LTS) using composer and a lamp server using $ sudo apt-get install lamp-server^
command (I didn't install Homestead). I am using PhpStorm 8.0.3
as IDE.
我创建了三个路由和一个控制器. PagesController.php
文件如下所示:
I created three routes and a controller. The PagesController.php
file looks like this:
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.php
)时,它都可以正常工作,并向我显示Welcome to my homepage!
消息.但是,每当我转到http://localhost:63342/my-first-app/public/hello
或http://localhost:63342/my-first-app/public/about
时,得到的都是404 Not Found
消息.
Whenever I go to http://localhost:63342/my-first-app/public/
(or http://localhost:63342/my-first-app/public/index.php
) it works fine and shows me the Welcome to my homepage!
message. But whenever I go to http://localhost:63342/my-first-app/public/hello
or http://localhost:63342/my-first-app/public/about
, what I get is 404 Not Found
message.
此外,位于my-first-app/public
的.htaccess
文件如下所示:
Also, the .htaccess
file which is located at my-first-app/public
looks like this:
<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/hello
或http://localhost:63342/my-first-app/public/index.php/about
,但是它也不起作用. - 我输入了命令
sudo a2enmod rewrite
,然后输入了sudo service apache2 restart
,但它也不起作用. - 我尝试了
composer dump-autoload
,但是它也不起作用. -
我在
apache2.conf
中将AllowOverride
从None
更改为All
.现在它的一部分看起来像这样:
- I tried
http://localhost:63342/my-first-app/public/index.php/hello
orhttp://localhost:63342/my-first-app/public/index.php/about
but it doesn't work either. - I entered command
sudo a2enmod rewrite
followed bysudo service apache2 restart
but it doesn't work either. - I tried
composer dump-autoload
but it doesn't work either. I changed
AllowOverride
fromNone
toAll
inapache2.conf
. Now part of it looks like this:
<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>
但它也不能解决问题.
更新(2015年7月15日):
运行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 | |
+--------+----------+-------+------+--------------------------------------------+------------+
推荐答案
我很长一段时间以来一直遇到这个烦人的问题.打开公用文件夹中的.htaccess
文件,并替换以下代码,以防万一.请备份原始代码.
I have been having this annoying problem for a long time. Open .htaccess
file in public folder and replace the following code, make a backup of your original code just in case.
<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>
这篇关于laravel 5仅根路由有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!