Lucas Futami
Lucas Futami

Reputation: 41

404 Not Found, what is wrong on Route and Controller

I was tried to find the problem, but still not found what is wrong on my code. Can anybody help what is wrong on my code.

route (web.php)

Route::get('/pegawai','PegawaiController@index');

PegawaiController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class PegawaiController extends Controller
{
    public function index()
    {
        //mengambil data dari table pegawai
        $pegawai = DB::table('pegawai')->get();
 
        // mengirim data pegawai ke view index
        return view('index',['pegawai' =>$pegawai]);

    }
}

My database is using mysql and for another program is can run just for this is 404 not Found. I'm using laravel 8.6

And I tried to create new project and is no problem, but on existing project is always 404 Not Found

Any idea why this happen??

Upvotes: 0

Views: 148

Answers (4)

Mohammad reza Golshahi
Mohammad reza Golshahi

Reputation: 307

change route to : Route::get('/pegawai', [PegawaiController::class, 'index']);

and check htaccess file exist in public folder

run php artisan cache:clear

Upvotes: 1

PilGyu Kang
PilGyu Kang

Reputation: 1

You should contain the namespace in Route::get facade function second parameter.

AS-IS

Route::get('/pegawai','PegawaiController@index');

TO-BE

Route::get('/pegawai','App\Http\Controllers\PegawaiController@index');

or

use App\Http\Controllers\PegawaiController;
Route::get('/pegawai', PegawaiController::class);

Upvotes: 0

Lucas Futami
Lucas Futami

Reputation: 41

I was solved this case, just code on command prompt php artisan route:cache and then my code is work

Upvotes: 0

PHP Geek
PHP Geek

Reputation: 4033

if you are using the latest version of laravel, then you should use the path of laravel in route like ---

Route::get('/pegawai',[\App\Http\Controllers\PegawaiController,'index']);

if show some error please remove \ in the controller path.

Hope it works for you.

Upvotes: 0

Related Questions