Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2016 版 Laravel 系列入门教程(三)【最适合中国人的 Laravel 教程】 #6

Closed
johnlui opened this issue Jun 6, 2016 · 41 comments

Comments

@johnlui
Copy link
Owner

johnlui commented Jun 6, 2016

本教程示例代码见:https://github.com/johnlui/Learn-Laravel-5

在任何地方卡住,最快的办法就是去看示例代码。

在本篇文章中,我们将尝试构建一个带后台的简单博客系统。我们将会使用到 路由、MVC、Eloquent ORM 和 blade 视图系统。

简单博客系统规划

我们在教程一中已经新建了一个 Eloquent 的 Model 类 Article,使用 migration 建立了数据表并使用 seeder 填入了测试数据。我们的博客系统暂时将只管理这一种资源:后台需要使用账号密码登录,进入后台之后,可以新增、修改、删除文章;前台显示文章列表,并在点击标题之后显示出文章全文。

下面我们正式开始。

搭建前台

前台的搭建是最简单的,我先带领大家找找感觉。

修改路由

删掉

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

将 /home 那一行修改为 Route::get('/', 'HomeController@index');,现在我们系统的首页就落到了 App\Http\Controllers\HomeController 类的 index 方法上了。

查看 HomeController 的 index 函数

learnlaravel5/app/Http/Controllers/HomeController.php 的 index 函数只有一行代码:return view('home');,这个很好理解,返回名字叫 home 的视图给用户。这个视图文件在哪里呢?在 learnlaravel5/resources/views/home.blade.php,blade 是 Laravel 视图系统的名字。

blade 浅析

blade 会对视图文件进行预处理,帮我们简化一些重复性很高的 echo、foreach 等 PHP 代码。blade 还提供了一个灵活强大的视图组织系统。打开 home.blade.php :

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>

                <div class="panel-body">
                    You are logged in!
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@extends('layouts.app')

这表示此视图的基视图是 learnlaravel5/resources/views/layouts/app.blade.php 。这个函数还隐含了一个小知识:在使用名称查找视图的时候,可以使用 . 来代替 / 或 \。

@section('content') ... @endsection

这两个标识符之间的代码,会被放到基视图的 @yield('content') 中进行输出。

访问首页

访问 http://fuck.io:1024 ,不出意外的话,你会看到这个页面:

为什么需要登录呢?怎么去掉这个强制登录呢?删掉 HomeController 中的构造函数即可:

public function __construct()
{
    $this->middleware('auth');
}

这个函数会在控制器类初始化的时候自动载入一个名为 auth 的中间件,正式这一步导致了首页需要登录。删除构造函数之后,重新访问 http://fuck.io:1024 ,页面应该就会直接出来了。这里要注意两点:① 一定要重新访问,不要刷新,因为此时页面的 url 其实是 http://fuck.io:1024/login ② 这个页面跟之前的欢迎页虽然看起来一毛一样,但其实文字是不同的,注意仔细观察哦。

向视图文件输出数据

既然 Controller - View 的架构已经运行,下一步就是引入 Model 了。Laravel 中向视图传数据非常简单:

public function index()
{
    return view('home')->withArticles(\App\Article::all());
}

修改视图文件

修改视图文件 learnlaravel5/resources/views/home.blade.php 的代码为:

@extends('layouts.app')

@section('content')
    <div id="title" style="text-align: center;">
        <h1>Learn Laravel 5</h1>
        <div style="padding: 5px; font-size: 16px;">Learn Laravel 5</div>
    </div>
    <hr>
    <div id="content">
        <ul>
            @foreach ($articles as $article)
            <li style="margin: 50px 0;">
                <div class="title">
                    <a href="{{ url('article/'.$article->id) }}">
                        <h4>{{ $article->title }}</h4>
                    </a>
                </div>
                <div class="body">
                    <p>{{ $article->body }}</p>
                </div>
            </li>
            @endforeach
        </ul>
    </div>
@endsection

刷新

如果你得到以上页面,恭喜你,Laravel 初体验成功!

调整视图

前台页面是不应该有顶部的菜单栏的,特别是还有注册、登录之类的按钮。修改视图文件为:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Learn Laravel 5</title>

    <link href="//cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="//cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
    <script src="//cdn.bootcss.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>

    <div id="title" style="text-align: center;">
        <h1>Learn Laravel 5</h1>
        <div style="padding: 5px; font-size: 16px;">Learn Laravel 5</div>
    </div>
    <hr>
    <div id="content">
        <ul>
            @foreach ($articles as $article)
            <li style="margin: 50px 0;">
                <div class="title">
                    <a href="{{ url('article/'.$article->id) }}">
                        <h4>{{ $article->title }}</h4>
                    </a>
                </div>
                <div class="body">
                    <p>{{ $article->body }}</p>
                </div>
            </li>
            @endforeach
        </ul>
    </div>

</body>
</html>

此视图文件变成了一个独立视图,不再有基视图,并且将 jQuery 和 BootStrap 替换为了国内的 CDN,更快更稳定了。

同理我们修改 learnlaravel5/resources/views/layouts/app.blade.php 为如下代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Laravel</title>

    <link href="//cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="//cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
    <script src="//cdn.bootcss.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body id="app-layout">
    <nav class="navbar navbar-default navbar-static-top">
        <div class="container">
            <div class="navbar-header">

                <!-- Collapsed Hamburger -->
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
                    <span class="sr-only">Toggle Navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <!-- Branding Image -->
                <a class="navbar-brand" href="{{ url('/') }}">
                    Laravel
                </a>
            </div>

            <div class="collapse navbar-collapse" id="app-navbar-collapse">
                <!-- Left Side Of Navbar -->
                <ul class="nav navbar-nav">
                    <li><a href="{{ url('/home') }}">Home</a></li>
                </ul>

                <!-- Right Side Of Navbar -->
                <ul class="nav navbar-nav navbar-right">
                    <!-- Authentication Links -->
                    @if (Auth::guest())
                        <li><a href="{{ url('/login') }}">Login</a></li>
                        <li><a href="{{ url('/register') }}">Register</a></li>
                    @else
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                {{ Auth::user()->name }} <span class="caret"></span>
                            </a>

                            <ul class="dropdown-menu" role="menu">
                                <li><a href="{{ url('/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li>
                            </ul>
                        </li>
                    @endif
                </ul>
            </div>
        </div>
    </nav>

    @yield('content')

</body>
</html>

接下来我们来着手搭建后台。

搭建后台

生成控制器

我们使用 Artisan 工具来生成控制器文件:

php artisan make:controller Admin/HomeController

成功之后,我们就可以看到 artisan 帮我们建立的文件夹及控制器文件了:

增加路由

我们要使用路由组来将后台页面置于“需要登录才能访问”的中间件下,以保证安全:

Route::group(['middleware' => 'auth', 'namespace' => 'Admin', 'prefix' => 'admin'], function() {
    Route::get('/', 'HomeController@index');
});

上一篇文章中我们已经接触到了路由组,这是 Laravel 的另一个伟大创造。路由组可以给组内路由一次性增加 命名空间、uri 前缀、域名限定、中间件 等属性,并且可以多级嵌套,异常强大。路由组中文文档在此:http://laravel-china.org/docs/5.1/routing#route-groups

上面的三行代码的功能简单概括就是:访问这个页面必须先登录,若已经登录,则将 http://fuck.io:1024/admin 指向 App\Http\Controllers\Admin\HomeController 的 index 方法。其中需要登录由 middleware 定义, /admin 由 prefix 定义,Admin 由 namespace 定义,HomeController 是实际的类名。

构建后台首页

新建 index 方法

public function index()
{
    return view('admin/home');
}

新建视图文件

learnlaravel5/resources/views/ 目录下新建一个名为 admin 的文件夹,在 admin 内新建一个名为 home.blade.php 的文件,填入代码:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>

                <div class="panel-body">

                    <a href="{{ url('admin/article') }}" class="btn btn-lg btn-success col-xs-12">管理文章</a>

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

修改 Auth 系统登陆成功之后的跳转路径

修改 learnlaravel5/app/Http/Controllers/Auth/AuthController.php 中的相应代码为:

protected $redirectTo = 'admin';

尝试登录

访问 http://fuck.io:1024/admin ,它会跳转到登陆界面,输入邮箱和密码之后,你应该会看到如下页面:

恭喜你,后台首页搭建完成!下面我们开始构建 Article 的后台管理功能。

构建 Article 后台管理功能

让我们先尝试点一下 “管理文章”按钮,不出意外你将得到一个 404 的报错:

进步之道

很多新手看到这个报错直接就慌了:什么鬼?全是英文看不懂呀。然后在文章下面把完整的错误栈全部粘贴出来。老实说我第一次见到 Laravel 报这个错也是完全没耐心去读,不过我还是复制了最明显的那个词“NotFoundHttpException”去 Google 了一下,从此我就再也没搜索过它了。

我为我的浮躁感到羞愧。那句话说的太对了:大多数人的努力程度之低,完全没有到拼天赋的程度。愿本教程的读者都做“少数人”。

添加路由

404 错误是访问了系统没有监听的路由导致的。下面我们要添加针对 http://fuck.io:1024/admin/article 的路由:

Route::group(['middleware' => 'auth', 'namespace' => 'Admin', 'prefix' => 'admin'], function() {
    Route::get('/', 'HomeController@index');
    Route::get('article', 'ArticleController@index');
});

刷新,错误变了:

新建控制器

上图中的报错是控制器不存在。我们使用 Artisan 来新建控制器:

php artisan make:controller Admin/ArticleController

刷新,错误又变了:

index 方法不存在。让我们新增 index 方法:

public function index()
{
    return view('admin/article/index')->withArticles(Article::all());
}

新建视图

上面我们已经新建过视图,现在应该已经轻车熟路了。在 learnlaravel5/resources/views/admin 下新建 article 文件夹,在文件夹内新建一个 index.blade.php 文件,内容如下:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">文章管理</div>
                <div class="panel-body">
                    @if (count($errors) > 0)
                        <div class="alert alert-danger">
                            {!! implode('<br>', $errors->all()) !!}
                        </div>
                    @endif

                    <a href="{{ url('admin/article/create') }}" class="btn btn-lg btn-primary">新增</a>

                    @foreach ($articles as $article)
                        <hr>
                        <div class="article">
                            <h4>{{ $article->title }}</h4>
                            <div class="content">
                                <p>
                                    {{ $article->body }}
                                </p>
                            </div>
                        </div>
                        <a href="{{ url('admin/article/'.$article->id.'/edit') }}" class="btn btn-success">编辑</a>
                        <form action="{{ url('admin/article/'.$article->id) }}" method="POST" style="display: inline;">
                            {{ method_field('DELETE') }}
                            {{ csrf_field() }}
                            <button type="submit" class="btn btn-danger">删除</button>
                        </form>
                    @endforeach

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

刷新,错误又变了:

Article 类不存在?原因很简单:Article 类和当前控制器类不在一个命名空间路径下,不能直接调用。解决办法就是主动导入 \App\Article 类:

... ...
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Article;

class ArticleController extends Controller
{
... ...

如果你还不熟悉命名空间,可以参考《PHP 命名空间 解惑》

检查成果

再次刷新,你应该能看到如下画面:

如果你没到这个画面也不用担心,根据他的错误提示去 Google 吧,一定能解决的。

新增、编辑、删除功能怎么办?

这三个功能我将在下一篇教程与大家分享,这是 2015 版 Laravel 教程做的不够好的地方,其实这里才是最应该掰开揉碎仔细讲解的地方。

下一步:2016 版 Laravel 系列入门教程(四)【最适合中国人的 Laravel 教程】

@keaixiaou
Copy link

博主用的是哪个编辑器,用的是啥主题

@johnlui
Copy link
Owner Author

johnlui commented Jun 13, 2016

@keaixiaou Sublime Text 3,Spacegray 主题。这个主题是编辑器主题,不是代码配色。

@wrh910201
Copy link

建立后台那部分,我有个疑问。按照博主的做法,新建了Admin/HomeController,但是实际上访问的依然是HomeController这个Controller,新建的Controller实际上并没有用到。删掉Admin/Controller,会抛出异常 ‘Class App\Http\Controllers\Admin\HomeController does not exist’。是不是路由那里配置有点问题?刚学Laravel,望不吝赐教!谢谢
环境:PHP5.5.9,nginx1.6,mysql5.5,laravel5.2

@johnlui
Copy link
Owner Author

johnlui commented Jun 27, 2016

@wrh910201 Admin/HomeController 中间的 Admin 部分写在了路由组里,并不是“新建的Controller实际上并没有用到”

@wrh910201
Copy link

当我访问fuck.io/admin这个url的时候,我在ControllerDispatcher.php这个文件var_dump($controller),输出的是string(35) "App\Http\Controllers\HomeController"。在Admin/HomeController的index()方法里修改页面没有变化,在HomeController的index()方法修改页面相应变化。
我的意思是:
1、您这篇文章里路由组的配置是不是本身就是访问到HomeController,而不是Admin/HomeController;
2、如果如1所说,如何配置访问到Admin/HomeController;
3、如果您实际上就是访问的就是Admin/HomeController,可能是我这边配置的问题,我再回头重新配置试一下。
谢谢您的回复!

@johnlui
Copy link
Owner Author

johnlui commented Jun 28, 2016

@wrh910201 明白了哈哈,你这个应该就是 Route::group 里面 namespace 设置的问题

@lin1987
Copy link

lin1987 commented Jun 29, 2016

@johnlui
谢谢博主的教程,一路下来没有出现错误,只是有点疑问,请帮忙解答。
教程三最后说需要添加声明 命名空间 use App\Article; ,为什么前面的 Admin/HomeController 不需要添加?添加的命名空间为什么是这样的,命名空间 App 应该在某个地方定义过,是在哪里呢?

@johnlui
Copy link
Owner Author

johnlui commented Jun 29, 2016

@lin1987 因为用的是绝对命名空间

public function index()
{
    return view('home')->withArticles(\App\Article::all());
}

@lin1987
Copy link

lin1987 commented Jun 29, 2016

谢谢博主,理解了,教程一中讲到,在使用 Artisan 工具新建 Model 类时,这个类文件中创建了 命名空间 \App。

@zhuowenji
Copy link

auth 可以绑定2个model吗 ? 就是后台管理员登录走admins表 前台用户登录走 users表。

@liuhaochuan79
Copy link

liuhaochuan79 commented Jul 1, 2016

public function index()
{
return view('home')->withArticles(\App\Article::all());
}

这里的withArticles是哪里定义的?我翻遍了所有代码都找不到定义,是语法糖么? 该如何理解?

还有其他的写法吗? 这种写法感觉很奇怪的样子。。。

更新 一下

return view('home')->with('Articles', \App\Article::all()); 这样写貌似好理解多了,新手勿嘲

@CaesarChan
Copy link

我在使用Auth的时候 更改验证后的跳转 protected $redirectTo = 'admin'; 但是登录过后 就是循环重定向。没有进入到 后台管理页面。前面流程都很顺畅。希望有楼主能解答下!

@netcan
Copy link

netcan commented Jul 31, 2016

同问,withArticles的用法。

@cy123
Copy link

cy123 commented Jul 31, 2016

我也是,在使用Auth的时候跳转 protected $redirectTo = 'admin'; 但是登录过后 ,就是循环重定向

@wuhuifeng
Copy link

@liuhaochuan1 这个好像写错了吧return view('home')->with('Articles', \App\Article::all());
应该是return view('home')->with('articles', \App\Article::all());才对。

@canary920
Copy link

@wuhuifeng 尝试了一下 确实是需要小写才正常 第一个参数应该是渲染到blade模板的变量名称
和模板中的 @foreach ($articles as $article) 这个变量名称是对应的

@Jyhwenchai
Copy link

5.3中没有AuthController该如何配置呢?

@Tianbatua
Copy link

@Jyhwenchai 有loginController

@zl02655931
Copy link

博主,按照你的教程做到路由群组那里,无论怎么登录,在登录后都会跳转到/login,已在authcontroller里绑定了跳转到admin。另外,如果我单独输入/admin会直接跳转到/。而路由群组那里我已经用了prefix。求指教

@johnlui
Copy link
Owner Author

johnlui commented Dec 28, 2016

@zl02655931 你可能没登录成功

@kongcheng123
Copy link

kongcheng123 commented Jan 23, 2017

你好,我遇到了和上面wrh910201同样的问题,根据你的步骤,localhost:8000/admin会跳到下面
Route::group(['middleware' => 'auth', 'namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::get('/', 'HomeController@index');
});
然后,会跳到HomeController,而不是Admin/HomeController,这是我的配置有问题吗

@uchiyou
Copy link

uchiyou commented Feb 26, 2017

同问,withArticles的用法!拜托了.

@LLawliet444
Copy link

@kongcheng123 感觉是中间件的问题,你有没有检查过你的 app/Http/Controllers/Middleware/Authenticate.php文件?

@JoeOvan
Copy link

JoeOvan commented Feb 27, 2017

@wrh910201 @kongcheng123 我遇到了和你们同样的问题,根据你的步骤,localhost:8000/admin会跳到下面
Route::group(['middleware' => 'auth', 'namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::get('/', 'HomeController@index');
});
然后,会跳到HomeController,而不是Admin/HomeController,这是我的配置有问题吗 你们的问题解决了吗?是怎么回事呀

你们的问题解决了吗?

@LoveCppp
Copy link

LoveCppp commented Mar 2, 2017

Route::group(['middleware' => ['web','auth'], 'namespace' => 'Admin', 'prefix' => 'admin'], function() {
    Route::auth();  //加上这个之后变不会再跳到 HomeController 而是 Admin/HomeController
    Route::get('/', 'HomeController@index');

    Route::get('article', 'ArticleController@index');
});

@jensonlau8086
Copy link

Route::group(['middleware' => ['web','auth'], 'namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::auth(); //加上这个之后变不会再跳到 HomeController 而是 Admin/HomeController
Route::get('/', 'HomeController@index');

Route::get('article', 'ArticleController@index');

});
根据t4mo的说明加上这个还是不行啊。到底要怎么样才能跳转到Admin/HomeController,快疯了

@LoveCppp
Copy link

LoveCppp commented Mar 29, 2017

@jensonlau8086 不知道你怎么写的 https://github.com/t4mo/laravel-blog/blob/master/app/Http/routes.php 可以看看我的代码

@jensonlau8086
Copy link

@t4mo 感谢你啦,我重来一次居然成功了。

@wenju319
Copy link

wenju319 commented Apr 1, 2017

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::auth();
Route::get('/', 'HomeController@index');
});
不加入中间件,可以访问 Admin/HomeController ,加入后登陆成功后直接跳转到了HomeController,已在authcontroller里绑定了跳转到admin,请问会是神马原因呢

@jensonlau8086
Copy link

@wenju319 我是这样的
Route::auth();
Route::get('/', 'HomeController@index');

Route::group(['middleware' => ['auth'],'namespace' => 'Admin', 'prefix' => 'admin'], function () {
Route::get('/', 'HomeController@index');
Route::get('article', 'ArticleController@index');
});

@valiner
Copy link

valiner commented May 11, 2017

资瓷一下。

@PleaseCallMeXiuye
Copy link

同样问withArticles函数的定义(应该是写在哪个文件呢?)
return view('home')->withArticles(\App\Article::all());
会报找不到函数withArticles,然后改成了下面的方式就正常显示了
return view('home')->with('articles', \App\Article::all());

//详情可以看View.php的 view()的参数说明 @uchiyou @netcan
// @liuhaochuan1 照模板输出的变量,应该是小写的变量 articles

@iepngs
Copy link

iepngs commented Jun 12, 2017

后台那个页面是用的模板还是原生写的啊?

@TerryGeng
Copy link

TerryGeng commented Aug 12, 2017

@PleaseCallMeXiuye @uchiyou @netcan @liuhaochuan1
关于withArticles的问题,实际上view里面并没有定义一个withArticles的函数。这个函数是动态绑定出来的。源代码位于learnlaravel5/vendor/laravel/framework/src/Illuminate/View/View.php大概380行左右的样子,

    /**
     * Dynamically bind parameters to the view.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return \Illuminate\View\View
     *
     * @throws \BadMethodCallException
     */
    public function __call($method, $parameters)
    {
        if (Str::startsWith($method, 'with')) {
            return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
        }

        throw new BadMethodCallException("Method [$method] does not exist on view.");
    }

其中,Str::snake是一个字符串处理函数,作用是

Convert a string from camel case format to snake case.

所以实际上
view('home')->withArticles(\App\Article::all())等价于view('home')->with('articles', \App\Article::all())

@TerryGeng
Copy link

关于Route::group(['middleware' => ['web','auth'], 'namespace' => 'Admin', 'prefix' => 'admin']这句
附上一个跟Controller@Action有关的tips:

Controllers & Namespaces
It is very important to note that we did not need to specify the full controller namespace when defining the controller route. Since the RouteServiceProvider loads your route files within a route group that contains the namespace, we only specified the portion of the class name that comes after the App\Http\Controllers portion of the namespace.

上面代码里那个 'namespace' => 'Admin'实际上是App\Http\Controllers\Admin,如果不写 'namespace' => 'Admin'而是直接在get里写的话用Admin\HomeController@index其实效果是一样的。

@F4NNIU
Copy link

F4NNIU commented Aug 26, 2017

好教程,非常感谢。

这里少了一个 body 麻烦帮忙加上。

调整视图
···

<title>Learn Laravel 5</title>

<link href="//cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="//cdn.bootcss.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<div id="title" style="text-align: center;">
    <h1>Learn Laravel 5</h1>
    <div style="padding: 5px; font-size: 16px;">Learn Laravel 5</div>
</div>
<hr>
<div id="content">

···

@HanielF
Copy link

HanielF commented Sep 3, 2017

Laravel Version :5.4.33

In section of Background Build .

Route :
Route::group(['middleware' => ['web','auth'], 'namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::auth();
Route::get('/', 'HomeController@index');
});

Other configurations are all same of the tutorial's .
File /Admin/HomeController.php exists and index function was defined.
The view file is all right too.

But when i visit 127.0.0.1/admin, It always shows "File not found".
image

What would result in this problem? And how can i fix it?

@johnlui
Copy link
Owner Author

johnlui commented Sep 4, 2017

@HanielF 没用 php -S 启动吧?你遇到的是伪静态问题:127.0.0.1/index.php/admin 可解

@HanielF
Copy link

HanielF commented Sep 4, 2017

@johnlui Thank you very much . It works.

@developbiao
Copy link

新版本的一直没有找到AuthController,原来是在Controllers/Auth/LoginController.php 文件中定义登陆成功后跳转。

@johnlui
Copy link
Owner Author

johnlui commented Sep 4, 2017

@developbiao 基于 Laravel 5.5 的 2017 版教程正在快马加鞭地编写,敬请期待~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests