路线问题:Laravel 5.8中不存在类控制器

时间:2019-07-18 05:20:19

标签: php laravel laravel-5 controller routes

我正在创建个人资料部分。 show.blade.php是个人资料编辑部分。

但是我看不到show.blade.php.

我遇到了以下错误。

error image

这是我的代码。

  

web.php

Route::resource('channels', 'ChannelController');

php artisan route:在终端列表。这就是结果。 php artisan route:list

app.blade.php 单击此处后,我可以跳到show.blade.php

<a class="dropdown-item" href="{{ route('channels.show', auth()->user()->channel->id) }}"> My Channel</a>

controller.php

<?php

namespace Laratube\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

ChannelController.php

    <?php

namespace Laratube\Http\Controllers;

use Illuminate\Http\Request;

class ChannelController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Channel $channel)
    {
        return view('channels.show', compact('channel'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
  

show.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    {{ $channel->name }}
                </div>

                <div class="card-body">
                    <form id="update-channel-form" action="{{ route('channels.update', $channel->id) }}" method="POST" enctype="multipart/form-data">
                        @csrf

                        @method('PATCh')
                        <div class="form-group">   
                            <label for="name" class="form-control-label">
                                Name
                            </label>
                            <input id="name" name="name" value="{{ $channel->name }}" type="text" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="description" class="form-control-label">
                                Description
                            </label>
                            <textarea name="description" id="description" cols="3" rows="3" class="form-control">
                                {{ $channel->description }}
                            </textarea>
                        </div>

                        <button class="btn btn-info" type="submit">Update</button>

                    </form>

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

我尝试了以下操作。

    php artisan cache:clear

    composer update

    composer dump-autoload

    php artisan db:seed

但是仍然无法正常工作。

很高兴有人帮我。

我想我的路线有问题。这条路线也不起作用。

Route::resource('channels', 'ChannelController')->name('channels.show');

3 个答案:

答案 0 :(得分:1)

    //In Your Controller
    <?php

    namespace Laratube\Http\Controllers;

    use Laratube\Channel;
    use Illuminate\Http\Request;

    class ChannelController extends Controller
    {


//in show function
public function show()
    {
        $channel = Channel::get();
        return view('channels.show', compact('channel'));
    }

答案 1 :(得分:0)

我的猜测(没有您的控制器代码)是您没有导入模型Channel。消息很清楚,它尝试从其当前目录Laratube\Http\Controllers\中检索模型类。

答案 2 :(得分:0)

您没有在控制器中导入通道模型。 在控制器的顶部使用部分添加该行。

use Laratube\Channel;