Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

度々申し訳ございません。laravelのアプリケーション作成で、Target class [HomeController] does not exist.というエラーが発生してしまいました。

解決したいこと

度々申し訳ございません。laravelのアプリケーション作成で、Target class [HomeController] does not exist.というエラーが発生してしまいました。
こちらのエラーについて知見のある方はご教授いただきたいです。
よろしくお願いいたします。
参考にした記事はこちらでございます。

https://laracasts.com/discuss/channels/laravel/laravel8-ui-bootstrap-uiauth-error-target-class-homecontroller-does-not-exist
https://laracasts.com/discuss/channels/laravel/laravel-8-error-target-class-homecontroller-does-not-exist

エラー文.png

発生している問題・エラー

Illuminate\Contracts\Container\BindingResolutionException
Target class [HomeController] does not exist.
http://localhost:8000/home


Illuminate\Container\Container::build
vendor/laravel/framework/src/Illuminate/Container/Container.php:832

     *

     * @throws \Illuminate\Contracts\Container\BindingResolutionException

     */

    public function build($concrete)

    {

        // If the concrete type is actually a Closure, we will just execute it and

        // hand back the results of the functions, which allows functions to be

        // used as resolvers for more fine-tuned resolution of these objects.

        if ($concrete instanceof Closure) {

            return $concrete($this, $this->getLastParameterOverride());

        }



        try {

            $reflector = new ReflectionClass($concrete);

        } catch (ReflectionException $e) {

            throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);

        }



        // If the type is not instantiable, the developer is attempting to resolve

        // an abstract type such as an Interface or Abstract Class and there is

        // no binding registered for the abstractions so we need to bail out.

        if (! $reflector->isInstantiable()) {

            return $this->notInstantiable($concrete);

        }


該当するソースコード

post_controller.php

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::all();
        return view('posts.index', ['posts' => $posts]);
    }

    /**
     * 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)
    {
        $id = Auth::id()
        // インスタンス作成
        // エラーが発生していてデータベースが作成できていないためエラーが出る
        $post = new Post();


        $post->body = $request->$body;
        $post->user_id = $id;

        $post->save();
        // $post -> new Post

        return redirect()->to('/posts');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function show(Post $post)
    {
        $user_id = $post->user_id;
        // sql文を書きユーザーの情報をとっていきている
        $user = DB::table('users')->where('id', $user_id)->first();

        return view('post.detail',['post' => $post, 'user' => $user]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // Postモデルのidを$postに代入
        $post = Post::findOrFail($id);
        // viewに返り値
        return view('post.edit',['post' => $post]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //レコード検索
        $post = Post::findOrFail($id);

        $post->body = $request->body;

        //保存(更新)
        $post->save();

        return redirect()->to('/posts');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $post = Post::find($id);
        $post->delete();

        return redirect()->to('/posts');
    }
}

User_controller.php


<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController 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  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        //
    }

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

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

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

Home.contoller.php

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::all();
        return view('posts.index', ['posts' => $posts]);
    }

    /**
     * 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)
    {
        $id = Auth::id()
        // インスタンス作成
        // エラーが発生していてデータベースが作成できていないためエラーが出る
        $post = new Post();


        $post->body = $request->$body;
        $post->user_id = $id;

        $post->save();
        // $post -> new Post

        return redirect()->to('/posts');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function show(Post $post)
    {
        $user_id = $post->user_id;
        // sql文を書きユーザーの情報をとっていきている
        $user = DB::table('users')->where('id', $user_id)->first();

        return view('post.detail',['post' => $post, 'user' => $user]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // Postモデルのidを$postに代入
        $post = Post::findOrFail($id);
        // viewに返り値
        return view('post.edit',['post' => $post]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //レコード検索
        $post = Post::findOrFail($id);

        $post->body = $request->body;

        //保存(更新)
        $post->save();

        return redirect()->to('/posts');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $post = Post::find($id);
        $post->delete();

        return redirect()->to('/posts');
    }
}

追記いたします! @ragner_k さんから、web.phpファイルを確認したいとのことなので下記に記載します。

お手数おかけしますがご確認お願いいたします。

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('posts', 'PostController', ['only' => ['index','show', 'create', 'store']]);
Route::get('posts/edit/{id}', 'PostController@edit');
Route::post('posts/edit', 'PostController@update');
Route::post('posts/delete/{id}', 'PostController@destroy');

また、足りない情報やご指摘があればお願いいたします。
よろしくお願いいたします!

0 likes

3Answer

大体の場合は次のケースです。

  • ファイル名、ディレクトリ名の間違い
  • 名前空間(namespace)の間違い
  • クラス名(class)の間違い
  • ファイルが読み込まれていない(Autoloaderの対象外になっているなど)

PostControllerHomeControllerではないでしょうか?

Home.contoller.php
class PostController extends Controller
2Like

下記サイトの「コントローラ名前空間の自動プレフィクス」を参照のとおり、対応が2パターンあります。

1;web.phpにコントローラーへのuseを記載する
2;RouteServiceProviderに$namespaceを指定する

一度どちらか試してみて頂けますか?

また、web.phpはどのように記述されていますでしょうか?

0Like

Comments

  1. @suiru_nakamura

    Questioner

    ご回答ありがとうございます
    ご返信遅くなってしまい大変申し訳ございません。
    web.phpですが、初めの投稿画面に載せましたので、確認のほどよろしくお願いいたします。

追記ありがとうございます。

内容見る限り、パスが通っていないようですね。
下記をweb.phpに追加してもらうことは可能ですか?

web.php
use App\Http\Controllers\HomeController

もしくは、RouteServiceProviderクラスに下記を追加してもらうことは可能ですか?

RouteServiceProvider.php
protected $namespace = 'App\Http\Controllers';
0Like

Your answer might help someone💌