php Laravel 快速入门指南路线不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16897504/
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
Laravel quick start guide route not working
提问by twigg
Ok I'm new to Laravel so went straight to the documentation to get started. There are massive holes in the documentation so it took a lot of effort and googling to fill the gaps in order to get Laravel set-up. I now have it set up and moved on to the next step in the quick start guide.I created my route
好的,我是 Laravel 的新手,所以直接阅读文档以开始使用。文档中存在大量漏洞,因此需要花费大量精力和谷歌搜索来填补空白,以便设置 Laravel。我现在已经设置好并进入快速入门指南中的下一步。我创建了我的路线
Route::get('users', function()
{
return 'Users!';
});
Now it says:
现在它说:
Now, if you hit the /users route in your web browser, you should see Users!
So I hit up:
所以我打了起来:
http://localhost/laravel/users
but get a 404? I tried
但得到一个 404?我试过
http://localhost/laravel/public/users
but still a 404? I followed the steps on the quick start guide to the letter, what am I missing?
但仍然是 404?我按照快速入门指南中的步骤操作了这封信,我错过了什么?
回答by Rubens Mariuzzo
Seems like your Laravel app is accesible via an Apache HTTP alias, because your URL looks like: http://localhost/laravel/
. If this is the case and assumingthat http://localhost/laravel
is pointing to your public directory, then follow these steps:
好像你Laravel应用程序是通过的Apache HTTP别名入店,因为你的URL看起来像:http://localhost/laravel/
。如果是这种情况并假设它http://localhost/laravel
指向您的公共目录,请执行以下步骤:
- Try to navigate to your expected route prepend it with
/index.php/
, in your case:http://localhost/laravel/index.php/users
. If it works (no 404) then you problem is with the Rewrite Module configuration of Apache HTTP, you should follow the next steps. - Edit the file
public/.htaccess
. - Under the line
RewriteEngine On
addRewriteBase /laravel/
. - Try to navigate to an existing route.
- 尝试导航到您的预期路线
/index.php/
,在您的情况下:http://localhost/laravel/index.php/users
。如果它有效(没有 404),那么您的问题在于 Apache HTTP 的重写模块配置,您应该按照以下步骤操作。 - 编辑文件
public/.htaccess
。 - 在该行下
RewriteEngine On
添加RewriteBase /laravel/
. - 尝试导航到现有路线。
Basically, if you app resides in a alias or virtual directory (say http://localhost/alias
) you should add an entry in your rewrite rule to rewrite the base directorywith alias
.
基本上,如果你在一个别名或虚拟目录(比如应用程式所在http://localhost/alias
),你应该在你的重写规则添加一个条目来重写基本目录用alias
。
回答by ag0r0
Apache isn't probably reading the public/.htaccess file due to the directive AllowOverridebeing set to None. Try to set it to All.
由于指令AllowOverride被设置为None ,Apache 可能不会读取 public/.htaccess 文件。尝试将其设置为All。
Laravel 4 simple route not working using mod_rewrite, and .htaccess
回答by Hammad Khan
The problem is well explained by Rubens above, A simpler solution is to use the supplied built-in PHP Server by issuing this command
上面的鲁本斯很好地解释了这个问题,一个更简单的解决方案是通过发出此命令来使用提供的内置 PHP 服务器
php artisan serve --port=8080
Note that I am using port 8080 here, you can omit it.Now you can browse the site by going to
注意我这里使用的是8080端口,你可以省略它。现在你可以通过去浏览网站
localhost/users
and it should work!
它应该工作!
回答by gugabguerra
I had the same problem and spent hours to solve it.
I have several apps under the same domain, but in different segments. So Laravel's url is http://myhostname/mysubdir/
我遇到了同样的问题,花了几个小时来解决它。我在同一个域下有多个应用程序,但在不同的细分市场中。所以 Laravel 的 url 是http://myhostname/mysubdir/
My controllers were only working as http://myhostname/mysubdir/index.php/mycontroller
我的控制器只能作为 http://myhostname/mysubdir/index.php/mycontroller
On /var/www/.../public/.htaccess I put RewriteBase /mysubdir
and worked!!!
在 /var/www/.../public/.htaccess 上,我投入了RewriteBase /mysubdir
工作!!!
回答by Kiren Siva
I know this question is 4 years old but still it have its significance.Rubens Mariuzzo was answered it correctly but I want to add some points on it. You said
我知道这个问题已经有 4 年历史了,但它仍然有它的意义。Rubens Mariuzzo 的回答是正确的,但我想补充几点。你说
"There are massive holes in the documentation so it took a lot of effort and googling to fill the gaps in order to get Laravel set-up"
“文档中存在大量漏洞,因此需要花费大量精力和谷歌搜索来填补空白,以便设置 Laravel”
For beginners it is difficult to find correct way of configuring Laravel. Once it is mastered it is fun developing Laravel :) . There are certain correct way of doing this.
对于初学者来说,很难找到正确的 Laravel 配置方法。一旦掌握了它,开发 Laravel 就会很有趣:)。有一些正确的方法可以做到这一点。
- Download Laravel
- Configure DB
- Map DB in .env
- Make auth: php artisan make:auth
- Create model and migration together: php artisan make:model Todo -m
- Migrate: php artisan migrate
- Create controller and routes together: php artisan make:controller TodoController --resource
- Create view for each action
- Code the controller
- 下载 Laravel
- 配置数据库
- .env 中的映射数据库
- 制作身份验证:php artisan make:auth
- 一起创建模型和迁移:php artisan make:model Todo -m
- 迁移:php artisan 迁移
- 一起创建控制器和路由: php artisan make:controller TodoController --resource
- 为每个动作创建视图
- 编码控制器
Detailed description is given in this blog http://masterlaravel.blogspot.in/2017/08/laravelquick-start-composer-create.html
详细说明在这个博客http://masterlaravel.blogspot.in/2017/08/laravelquick-start-composer-create.html
回答by mr.von.chn
please refer to this follow url and see RoboTamer's config:
请参考此跟随网址并查看 RoboTamer 的配置:
http://forums.laravel.io/viewtopic.php?id=511
http://forums.laravel.io/viewtopic.php?id=511
it solved my problem the same as yours
它和你一样解决了我的问题
solution in nginx:
nginx中的解决方案:
server {
服务器 {
server_name .laravel.dev;
root /home/tamer/code/laravel/public;
index index.php index.html;
#browse folders if no index file
autoindex on;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ / permanent;
}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www ;
rewrite ^/(.*)$ $scheme://$host_without_www/ permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/lobby(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ / permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/ last;
break;
}
# catch all
error_page 404 /index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
#include fastcgi_params;
include /home/tamer/code/nginx/fastcgi_params;
}
access_log /home/tamer/code/laravel/storage/logs.access.log;
error_log /home/tamer/code/laravel/storage/logs.error.log;
}
}
回答by Harish
Thanks. Knowledge of the above by @rubens, and a comment by @GaryJ heremade me do this to make it work from my Apache virtual hosts file.
谢谢。以上知识由@rubens,并通过@GaryJ评论在这里让我这样做是为了让它从我的Apache虚拟主机文件的工作。
Copied these lines from .htaccess file in laravel within node of the vhosts config. Removed my .htaccess file (it was anyway not being applied, i am on a windows apache virtual host, laravel env)
从 vhosts 配置节点内的 laravel 中的 .htaccess 文件复制这些行。删除了我的 .htaccess 文件(无论如何它没有被应用,我在 windows apache 虚拟主机上,laravel env)
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Better: Later read @Ag0r0 's reply, that worked. i was missing the allow override, making the above neccessary. once allowoverride all was there, the default settings itself worked.
更好:稍后阅读@Ag0r0 的回复,这很有效。我错过了允许覆盖,使上述成为必要。一旦 allowoverride all 存在,默认设置本身就起作用了。
回答by Mücahit Y?lmaz
In my case (Ubuntu 14.04 LTS & Apache 2.2
) I was stuck at #1 step of @Rubens' solution. I could see the page with this url: http://localhost/laravel/index.php/users
but step #2 and others didn't work.
就我而言(Ubuntu 14.04 LTS & Apache 2.2
),我被困在@Rubens 解决方案的第 1 步。我可以看到带有这个 url 的页面:http://localhost/laravel/index.php/users
但是第 2 步和其他步骤不起作用。
Also I've tried to add RewriteBase /laravel/public
to .htaccess
but it didn't work too.
我也尝试添加RewriteBase /laravel/public
,.htaccess
但它也不起作用。
Then I've set up a new virtual host with the help of this awesome tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts(they rock with these tuts)
然后我在这个很棒的教程的帮助下设置了一个新的虚拟主机:https: //www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu- 14-04- lts(他们用这些嘟嘟声摇滚)
In addition to tutorial, I've set DocumentRoot
(in example.com.conf
file) like this:
除了教程之外,我还这样设置DocumentRoot
(在example.com.conf
文件中):
DocumentRoot /var/www/laravel/public
Yes, public
is the big deal here.
是的,public
这里很重要。
Btw, don't put RewriteBase /laravel/
to your .htacces
file otherwise you'll get HTTP/500
error.
顺便说一句,不要放入RewriteBase /laravel/
你的.htacces
文件,否则你会得到HTTP/500
错误。
Now, I can see example.com/users
page.
现在,我可以看到example.com/users
页面。
回答by vladimir dusek
my previous post with the describtion of the problem was deleted, but it was similar to the situation that is described here.
我之前描述问题的帖子被删除了,但它类似于这里描述的情况。
anyway... simply i have tried all the possibilities that are described here as well as on other pages and I did not find solution to my problem.
无论如何......只是我已经尝试了这里以及其他页面上描述的所有可能性,但我没有找到解决我的问题的方法。
The fresh installation of the Laraval simply did not worked.... after trying everythink from this post i decided to create yet another fresh installation and miracle happened this installation is actually working...
Laraval 的全新安装根本不起作用......在尝试了这篇文章中的everythink 之后,我决定创建另一个全新的安装,奇迹发生了这个安装实际上正在工作......
So my advice is: if you are not able to fix this problem just try to create another installation of the laravel and it might actually work.
所以我的建议是:如果你不能解决这个问题,就尝试创建另一个 Laravel 安装,它可能真的有效。
This might be actually the easiest way how to fix your problem in case you are starting from the scrach.
如果您从零开始,这实际上可能是解决问题的最简单方法。