Laravel blank white page with data load from JSON file - php

I'm trying to make sure my data is loading correctly into my laravel app. When I navigate to localhost/data it appears to be grabbing the data because it takes a while to load, but then just displays a blank white page. I'm wondering if my JSON file is too large for it to handle?
My SiteController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SiteController {
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);
}
}
?>
My routes:
<?php
use Illuminate\Support\Facades\Route;
//use App\Http\Controllers\Controller;
use App\Http\Controllers\SiteController;
Route::get('/data', [SiteController::class, 'index']);
Route::get('/', function () {
return view('welcome');
});
And here's the link to the JSON file I'm accessing. http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json
Thank you!!

Related

Laravel JSON file error message "target class does not exist"

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!!
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.

Laravel controller not existing

I wanna redirect to my controller instead of a view but it says: "Target class [app/Http/Controllers/MyFirstController] does not exist. "
here is the code (web.php file):
//just a view
Route::get('/', function () {
return view('index');
});
//just a view
Route::get('/final', function () {
return view('welcome');
});
//the controller is interested in
Route::get('/hello-controller', 'app/Http/Controllers/MyFirstController#index');
Controller code (app/Http/controllers/MyFirstController.php) :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyFirstController extends Controller
{
public function index(){
return "viva";
}
}
additional information:
Laravel Framework version: 8.83.17
PHP version : PHP 7.4.29
The namespace is not correct: Capital A for App and use \ instead of /
Route::get('/hello-controller', 'App\Http\Controllers\MyFirstController#index');
or even better :
Route::get('/hello-controller', [\App\Http\Controllers\MyFirstController::class, 'index']);
try to clear cache your controller by using
php artisan route:cache
and also maybe u should use
//the controller is interested in
Route::get('/hello-controller', 'App/Http/Controllers/MyFirstController#index');

Target class [app\Http\Controller\SocialController] does not exist.(when pressed on login with facebook )

Target class [app\Http\Controller\SocialController] does not exist.
when pressed on login with Facebook shown this error
my code is social controller and after press on login with face book button show me this upper error
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravel\socialite\Facades\Socialite;
class SocialController extends Controller
{
public function redirect($service)
{
return socialite::driver($service)->redirect();
}
public function callback($service)
{
return $user=socialite::with($service)->user();
}
}
my web page code is
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controller\SocialController;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('redirect/{service}',[SocialController::class,'redirect']);
Route::get('callback/{service}',[SocialController::class,'callback']);
You missed s in Controllers in web.php file. Correctly written would be:
use App\Http\Controllers\SocialController;

Laravel using Controller#show return view and api

I am making a web application that will also be turned into a native app so I want to show json from one path and return a view for another path. Here is what I was thinking but not sure how to write the if statements in Laravel:
Routes.php
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/notes', 'NotesController#index');
Route::get('/notes/{note}', 'NotesController#show');
Route::get('api/notes/{note}', 'NotesController#show');
});
NotesController.php
<?php
namespace App\Http\Controllers;
use App\Note;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class NotesController extends Controller
{
public function index() {
$notes = Note::all();
return view('notes.index', compact('notes'));
}
public function show(Note $note) {
//if its from the general path show view
return view('notes.show', compact('note'));
//if its from the api path show json
return $note;
}
}

Object not found error - When creating new pages in Laravel

I am new in Laravel. I am trying to create a new page named as "contact". But i am getting a Object not found error when i am trying to access the contact page
URL: project-name/contact
please help me
---routes file
<?php
Route::get('/','WelcomeController#index');
Route::get('contact','WelcomeController#contact');
Route::group(['middleware' => ['web']], function () {
//
});
--- Welcome controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class WelcomeController extends Controller
{
public function index(){
return view('welcome');
}
public function contact(){
return 'Contact page goes here...';
}
}
Set your home directory to the public to make things work.
Exchange the route. like this:
Route::get('contact','WelcomeController#contact');
Route::get('/','WelcomeController#index');

Categories