2

I have a controller named RankController.php. I rename in UsersController.php manually when I want to separate some methods. Then I choose to rename it back in to RankController.php. Note: I only change the name of the file, I don't change the name of the class. Here is the code of controller.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use App\User;
use App\Rank;
use App\Ban;

class RankController extends Controller
{

    // Constructor function
    public function __construct()
    {

        // Check if user is logged in
        $this->middleware('auth');
    }

    public function search(Request $request)
    {
        $this->validate($request, ['search' => 'required', ]);

        $users = User::select('id', 'name')->where([
            ['name', '!=', Auth()->user()->name],
            ['name', 'like', "%" . $request->input('search') . "%"], 
        ])->paginate(10);

        return view('rank.search')
            ->with('users', $users);
    }

}

And here is where I call the controller:

{!! Form::open(['action' => ['RankController@search'], 'method' => 'POST']) !!}
    {{Form::text('search', '', ['placeholder' => 'Search a user'])}}
    {{Form::submit('Search', ['class' => 'btn btn-primary'])}}
{!! Form::close() !!}

Note: This is the single file where I get the error. This is the error: Action App\Http\Controllers\RankController@search not defined. (View: /var/www/htdocs/Proiect/resources/views/layouts/app.blade.php) (View: /var/www/htdocs/Proiect/resources/views/layouts/app.blade.php)

I try the code on other PC and it generate the same error.

Kyaw Kyaw Soe
  • 3,258
  • 1
  • 14
  • 25
  • Have you updated route for "UsersController" ? – Pratik Sep 25 '18 at 07:27
  • 1
    If you change the name of your file you need to also change the name of your class. The [autoloading standard](https://www.php-fig.org/psr/psr-4/) works by using directory and filenames to represent namespaces and classes. If your filename differs from your class it cannot be autoloaded. – Jonathon Sep 25 '18 at 07:29
  • 1
    @Pratik that was the problem, I change it back now it's working thank you. – Triumfatorul32 Sep 25 '18 at 07:30
  • 1
    So can you please close/delete this question if it has been resolved or post/accept an answer? Thx – brombeer Sep 25 '18 at 07:47

2 Answers2

4

I thing you have to update route for "UsersController" and run "composer dumpautoload"

Hamid Naghipour
  • 3,465
  • 2
  • 26
  • 55
3

Make sure that the name of file and class are same and then try these commands:

1)php artisan route:cache
2)php artisan route:clear
3)composer dumpautoload -o
parastoo
  • 2,211
  • 2
  • 21
  • 38