gcpark

2521 经验值

@Tomoe
app.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="/css/bootstrap.css">
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
<nav class="navbar navbar-default navbar-static-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
            </button>
            <a class="navbar-brand" href="/">Laravel App</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/">Home</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                @if(Auth::check())
                    <li><a href="#">{ Auth::user()->name }</a></li>
                    <li><a href="/user/logout">Logout</a></li>
                    
                @else
                    <li><a href="/user/login">Login</a></li>
                    <li><a href="/user/register">Register</a></li>
                @endif
                
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</nav>
@yield('content')
</body>
</html>

create.blade.php

@extends('app')
@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-9" role="main">
                {!! Form::open(['url'=>'/discussions/create']) !!}
                <!--- Title Field --->
                <div class="form-group">
                    {!! Form::label('title', 'Title:') !!}
                    {!! Form::text('title', null, ['class' => 'form-control','placeholder' => 'Title']) !!}
                </div>
                <!--- Body Field --->
                <div class="form-group">
                    {!! Form::label('body', 'Body:') !!}
                    {!! Form::textarea('body', null, ['class' => 'form-control','placeholder' => 'Body']) !!}
                </div>
                <div>
                    {!! Form::submit('등록하기', ['class'=>'btn btn-primary form-control']) !!}
                </div>
                {!! Form::close() !!}
            </div>
        </div>
    </div>
@stop

@Tomoe 我也是纳闷 为什么 错误提示都没有 就不往下执行了。。。

route.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/


Route::resource('discussions', 'PostController');
Route::get('/', 'PostController@index');


Route::get('/user/register', 'UsersController@register');
Route::get('/user/login', 'UsersController@login');
Route::post('/user/register', 'UsersController@store');
Route::post('/user/login', 'UsersController@signin');
Route::get('/user/logout', 'UsersController@logout');

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Discussion;


class PostController extends Controller
{

    public function index()
    {

        $discussions = Discussion::all();
        return view('form.index',compact('discussions'));
    }

    public function create()
    {
        
        return view('form.create');
    }

    public function show($id)
    {
        $discussion = Discussion::findOrFail($id);
        return view('form.show', compact('discussion'));
    }


}

UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\User;
use Illuminate\Support\Facades\Auth;

class UsersController extends Controller
{
    public function login()
    {
        return view('users/login');
    }

    public function logout()
    {
        if(\Auth::check()) \Auth::logout();
        return redirect('/');
    }

    public function signin(Requests\UserLoginRequest $request)
    {
        if (\Auth::attempt([
            'email'=>$request->get('email'),
            'password'=>$request->get('password'),
            ])){
            return redirect('/');
        }
        \Session::flash('user_login_failed','이메일주소 혹은 비밀번호가 틀렸습니다.');
        return redirect('/user/login')->withInput();
    }

    public function register()
    {
            return view('users/register');
    }

    public function store(Requests\UserRegisterRequest $request)
    {
        User::create(array_merge($request->all(), ['avatar' => '/images/default.jpg']));
        //send email
        return redirect('/');
    }
}

在 FIRBUG上看 提示
error:
"NetworkError: 500 Internal Server Error - http://laravel.bbs/discussions/create"

@Tomoe 写法是肯定正确的 因为 首页里还有 /discussions/{id} 查看帖子 页面是 正常的 其余的 页面出错。。 也没有 出错信息

但为什么 创建帖子 视图里 Auth:check() 这个方法会出错呢 ? /discussion/? 这里又 不出错?


namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        //

        parent::boot($router);
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $this->mapWebRoutes($router);

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    protected function mapWebRoutes(Router $router)
    {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require app_path('Http/routes.php');
        });
    }
}

这是我的 App\Providers\RouteServiceProvider

我看 使用Laravel开发社区 这个视频 

跟着视频 学到 “实现登录”这一段 之前都很 顺利 跟着视频的代码 一个一个往下敲

但在 下一节视频上 创建 发布帖子的时候 发现 首页上 一切正常的 Auth:check() 一到 发布帖子页面就 出错了

在问答区 也搜索了 相关帖子 帖子上说是 定路由的时候 要用 middleware['web'] 

但我把路由放到 middleware 但什么都 不显示 一个错误提示都 没有 

真不知道 是什么问题。。。 所以 只能来这里 提问 望有 相关经验的人 帮我解答一下 

我用的 LARAVEL版本是 5.2