lnmp 安装 Laravel 设置路由不能访问

我在lnmp环境下用composer安装了laravel,用命令创建了blog项目,使用 http://ip/blog/public/ 可以正常访问,但设置了路由却访问不了路由,路由配置代码如下:

Route::resource('products', 'ProductController');

使用 http://ip/blog/public/products/create、http://ip/blog/public/index.php/products/create 这两个链接都报“404 Not Found”,调试已打开。

一开始我以为是路由配置错误,然后用以下代码测试也访问不了

Route::get('foo', function () {
    return 'Hello World';
})

请问有谁知道这个是什么原因?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 5

把项目根目录设置到 public 目录,然后访问 http://ip/

4年前 评论
22 (作者) 4年前
server {
    listen 80;
    listen 443 ssl http2;
    server_name .homestead.test;
    root "/home/vagrant/code/blog/public";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/homestead.test-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    location ~ /\.ht {
        deny all;
    }

    ssl_certificate     /etc/nginx/ssl/homestead.test.crt;
    ssl_certificate_key /etc/nginx/ssl/homestead.test.key;
}

homestead 的配置信息,你看下,是不是项目没有配置到public目录下

4年前 评论

兄弟,资源路由不是这么用的,资源路由是通过请求方式来断定是控制器中的哪个方法,如图:

file
建议你先把laravel的文档大致看完后再进行实操~

4年前 评论

@生活无限好 根目录配置了
@轻描淡写 vhost也配置了
@犯二青年 路由器《 Laravel 项目开发规范》 我看到这个文档,资源路由是可以这么配置的。现在改成了get配置也是不行的。
我再用其它方法试试,谢谢你们的回复! :blush:

4年前 评论

呀,我终于把路由弄出来了,路由配置还是没变,用的resource,只是我用lnmp vhost add 的方法新增了个项目(新增的项目目录 /home/wwwroot/blog),然后把之前blog(之前项目目录 /home/wwwroot/default/blog)的代码移到了新增网站的目录下,就可以了。

想来想去,也没明白是哪里出了问题了,对比了两次的vhost配置,是一样的。不过问题暂时解决啦。嘻嘻嘻 :grin: :grin:

再次谢谢你们的评论,让我想到了这个方法。

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!