0

I have a problem in route to access the Controller, i have copied laravel project version 5 to my project in laravel version 8, i have change the file RouteServiceProvider.php in my laravel project in to this

protected $namespace = 'App\Http\Controllers'; public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); }); } 

but still not working, the error message says "Illuminate\Contracts\Container\BindingResolutionException Target class [UserController] does not exist."

this is the web.php code

<?php use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('welcome'); }); // Auth::routes(); // Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); Route::get('/', [HomeController::class, 'index'])->name('home'); Auth::routes(); Route::group(['middleware' => 'web'], function(){ Route::get('user/profil', 'App\Http\Controllers\UserController@profil')->name('user.profil'); Route::patch('user/{id}/change', 'App\Http\Controllers\UserController@changeProfil'); Route::get('transaksi/baru', 'App\Http\Controllers\PenjualanDetailController@newSession')->name('transaksi.new'); Route::get('transaksi/{id}/data', 'App\Http\Controllers\PenjualanDetailController@listData')->name('transaksi.data'); Route::get('transaksi/cetaknota', 'App\Http\Controllers\PenjualanDetailController@printNota')->name('transaksi.cetak'); Route::get('transaksi/notapdf', 'App\Http\Controllers\PenjualanDetailController@notaPDF')->name('transaksi.pdf'); Route::post('transaksi/simpan', 'App\Http\Controllers\PenjualanDetailController@saveData'); Route::get('transaksi/loadform/{diskon}/{total}/{diterima}', 'App\Http\Controllers\PenjualanDetailController@loadForm'); Route::resource('transaksi', 'App\Http\Controllers\PenjualanDetailController'); }); 

this is for example the controller

<?php namespace App\Http\Controllers; use \Illuminate\Http\Response; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\User; use Auth; use Hash; class UserController extends Controller { public function index() { return view('user.index'); } public function listData() { $user = User::where('level', '!=', 1)->orderBy('id', 'desc')->get(); $no = 0; $data = array(); foreach($user as $list){ $no ++; $row = array(); $row[] = $no; $row[] = $list->name; $row[] = $list->email; $row[] = '<div class="btn-group"> <a onclick="editForm('.$list->id.')" class="btn btn-primary btn-sm"><i class="fa fa-pencil"></i></a> <a onclick="deleteData('.$list->id.')" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a></div>'; $data[] = $row; } $output = array("data" => $data); return response()->json($output); } public function store(Request $request) { $user = new User; $user->name = $request['nama']; $user->email = $request['email']; $user->password = bcrypt($request['password']); $user->level = 2; $user->foto = "user.png"; $user->save(); } public function edit($id) { $user = User::find($id); echo json_encode($user); } public function update(Request $request, $id) { $user = User::find($id); $user->name = $request['nama']; $user->email = $request['email']; if(!empty($request['password'])) $user->password = bcrypt($request['password']); $user->update(); } public function destroy($id) { $user = User::find($id); $user->delete(); } public function profil() { $user = Auth::user(); return view('user.profil', compact('user')); } public function changeProfil(Request $request, $id) { $msg = "succcess"; $user = User::find($id); if(!empty($request['password'])){ if(Hash::check($request['passwordlama'], $user->password)){ $user->password = bcrypt($request['password']); }else{ $msg = 'error'; } } if ($request->hasFile('foto')) { $file = $request->file('foto'); $nama_gambar = "fotouser_".$id.".".$file->getClientOriginalExtension(); $lokasi = public_path('images'); $file->move($lokasi, $nama_gambar); $user->foto = $nama_gambar; $datagambar = $nama_gambar; }else{ $datagambar = $user->foto; } $user->update(); echo json_encode(array('msg'=>$msg, 'url'=> asset('public/images/'.$datagambar))); } } 

this is the blade view for user.profil.blade for example

@extends('layouts.app') @section('title') Edit Profil @endsection @section('breadcrumb') @parent <li>user</li> <li>edit profil</li> @endsection @section('content') <div class="row"> <div class="col-xs-12"> <div class="box"> <form class="form form-horizontal" data-toggle="validator" method="post" enctype="multipart/form-data"> {{ csrf_field() }} {{ method_field('PATCH') }} <div class="box-body"> <div class="alert alert-info alert-dismissible" style="display:none"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> <i class="icon fa fa-check"></i> Perubahan berhasil disimpan. </div> <div class="form-group"> <label for="foto" class="col-md-2 control-label">Foto Profil</label> <div class="col-md-4"> <input id="foto" type="file" class="form-control" name="foto"> <br><div class="tampil-foto"> <img src="{{ asset('public/images/'.Auth::user()->foto) }}" width="200"></div> </div> </div> <div class="form-group"> <label for="passwordlama" class="col-md-2 control-label">Password Lama</label> <div class="col-md-6"> <input id="passwordlama" type="password" class="form-control" name="passwordlama"> <span class="help-block with-errors"></span> </div> </div> <div class="form-group"> <label for="password" class="col-md-2 control-label">Password</label> <div class="col-md-6"> <input id="password" type="password" class="form-control" name="password"> <span class="help-block with-errors"></span> </div> </div> <div class="form-group"> <label for="password1" class="col-md-2 control-label">Ulang Password</label> <div class="col-md-6"> <input id="password1" type="password" class="form-control" data-match="#password" name="password1"> <span class="help-block with-errors"></span> </div> </div> </div> <div class="box-footer"> <button type="submit" class="btn btn-primary pull-right"><i class="fa fa-floppy-o"></i> Simpan Perubahan</button> </div> </form> </div> </div> </div> @endsection @section('script') <script type="text/javascript"> $(function(){ $('#passwordlama').keyup(function(){ if($(this).val() != "") $('#password, #password1').attr('required', true); else $('#password, #password1').attr('required', false); }); $('.form').validator().on('submit', function(e){ if(!e.isDefaultPrevented()){ $.ajax({ url : "{{ Auth::user()->id }}/change", type : "POST", data : new FormData($(".form")[0]), dataType: 'JSON', async: false, processData: false, contentType: false, success : function(data){ if(data.msg == "error"){ alert('Password lama salah!'); $('#passwordlama').focus().select(); }else{ d = new Date(); $('.alert').css('display', 'block').delay(2000).fadeOut(); $('.tampil-foto img, .user-image, .user-header img').attr('src', data.url+'?'+d.getTime()); } }, error : function(){ alert("Tidak dapat menyimpan data!"); } }); return false; } }); }); </script> @endsection 
1
  • the error says that when you do what? looks like you also changed all your routes Commented Jan 4, 2021 at 1:54

3 Answers 3

3

You are having error because you are defining controller namespace on each route in your web.php file (Such as 'App\Http\Controllers\UserController@profil') as well as setting value for protected $namespace variable inside your RouteServiceProvider.php (Such as protected $namespace = 'App\Http\Controllers';).

Now what you need to do is either define controller namespace on each route in web.php file or provide value for protected $namespace variable inside your RouteServiceProvider.php

You can't do both things at same time. Just choose what suites best to your situation. Either remove the protected $namespace = 'App\Http\Controller'; from RouteServiceProvider.php file or remove controller namespaces that you have attached with each route in your web.php file. (Change Route::get('user/profil', 'App\Http\Controllers\UserController@profil')->name('user.profil'); to Route::get('user/profil', 'UserController@profil')->name('user.profil');) and everything should work fine.

For Laravel docs reference you can read https://laravel.com/docs/8.x/upgrade#routing

However many people would get this error because they are used to define their routes like Route::get('/users','UserController@index); and it was fine to define it this way in Laravel versions till Laravel 7. But Laravel 8 has changed this method and rather provides new way of defining Routes.

  • Using PHP callable syntax which is Route::get('/users', [UserController::class, 'index']); With this way you will need to import the controller file into the web.php

    OR

  • Using string syntax, which is Route::get('/users', 'App\Http\Controllers\UserController@index');

Sign up to request clarification or add additional context in comments.

Comments

2

If it's not your namespace problem, everything you have done by far doesn't work, maybe it's cache problem

 php artisan cache:clear; php artisan optimize php artisan route:list 

Comments

1

If you have made the namespace and route syntax changes, but aren't seeing any differences, make sure to run php artisan route:clear in the command line within your Laravel project's directory.

After a lot of searching, thank you to phoenix for his answer on a similar question, as I was having the same issue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.