0

I'm trying to load this JSON file from URL into my laravel project. It's showing this error from the host/data page. "Target class [App\Http\Controllers\SiteController] does not exist."

My routes web.php:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Controller;
use App\Http\Controllers\SiteController;


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




Route::get('/data', [SiteController::class, 'index']);

And my controller which is in app/http/Controllers/Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
  public function index() {
    $results = file_get_contents("http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json");
    $data = json_decode($results, true);
    dd($data);
    }
  }

Thank you!!

  • which version of your laravel? – Paul Mahardika Nov 03 '22 at 01:53
  • 4
    if your Controller was in `app/Http/Controllers/Controller` then the namespace would have to be `App\Http\Controllers\Controller` ... are you sure it isn't in `app/Http/Controllers`? and the filename would have to be `SiteController.php` – lagbox Nov 03 '22 at 02:05
  • I think you need to see here https://stackoverflow.com/questions/63807930/error-target-class-controller-does-not-exist-when-using-laravel-8 – Ismoil Shifoev Nov 03 '22 at 04:09

1 Answers1

0

Better move your SiteController to App\Http\Controllers directory and then everything will be fine. you may need read about namespaces in php. i hope this help you.

zarin
  • 7
  • 3
  • I changed the filename to SiteController and it is in the app/http/controlllers directory. It's giving me this error now: "Class "App\Http\Controllers\Controller" not found" and it seems to be highlighting this line "class SiteController extends Controller". Any thoughts? Thanks so much!! – Nicole Jaffke Nov 03 '22 at 22:28
  • you have to move also your controller.php file to the App\Http\Controller namespase also and take care about namespaces – zarin Nov 04 '22 at 15:17
  • I only have one controller which is now called SiteController. Do I need a second one? – Nicole Jaffke Nov 04 '22 at 17:40
  • you extend SiteController class from one class named Controller.php and also you use it in the top of your SiteController; can you share a picture from your directory levels in the Controllers folder? – zarin Nov 05 '22 at 07:05