Laravel controller not existing - php

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');

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.

why i have this wierd problem about my route

i'm working on a livewire project based on laravel ... every think was normal till yesterday my project didn't find some route and i recieved 404 .
for example one of my routes is /category/index
use App\Http\Livewire\Admin\Home as AdminPanel;
use App\Http\Livewire\AdminAuth\Register as AdminRegister;
use App\Http\Livewire\AdminAuth\Login as AdminLogin;
use App\Http\Livewire\AdminAuth\ForgotPassword as AdminForgotPassword;
use App\Http\Livewire\Category\Create as AddCategory;
use App\Http\Livewire\Category\Categories as Cats;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/home',AdminPanel::class)->middleware('auth')->name('Home');
Route::get('/admin/register',AdminRegister::class)->name('Admin.Register');
Route::get('/admin/login',AdminLogin::class)->name('Admin.Login');
Route::get('/admin/forgot-password',AdminForgotPassword::class)->name('Admin.ForgotPassword');
Route::get('/category/create',AddCategory::class)->name('create.category');
Route::get('/category/index',IndexCategory::class)->name('index.category');
my controller :
namespace App\Http\Livewire\Category;
use App\Models\Category;
use Livewire\Component;
use Livewire\WithPagination;
class Index extends Component
{
use WithPagination;
public $cat_id;
protected $listeners=['cat_id'=>'CatID'];
public function CatID($id){
$this->cat_id=$id;
}
public function render()
{
$cat=Category::findOrFail($this->cat_id);
$categories=Category::with('childrenRecursive')->where('parent_id',null)->paginate(15);
return view('livewire.category.index',compact('categories','cat'))->extends('layouts.admin')->section('content');
}
}
i tried all optimization artisan code like clear cache and clear route but it didn't work

Illuminate\Contracts\Container\BindingResolutionException Target class [app\Http\Controllers\FrontEnd\IndexController] does not exist

**Target class ** does not exist. ?? Why I didn't understand
Error Is lluminate\Contracts\Container\BindingResolutionException
Target class [app\Http\Controllers\FrontEnd\IndexController] does not exist.
Illuminate\Container\Container::build
C:\xampp\htdocs\check-time.com\vendor\laravel\framework\src\Illuminate\Container\Container.php:875
I am Using Laravel 8. Environment information
Laravel version
8.47.0
Laravel locale
en
Laravel config cached
true
PHP version
8.0.6
<?php
namespace App\Http\Controllers\FrontEnd;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class IndexController extends Controller
{
public function UserGuide(){
return view('FrontEnd.FrontWeb.User-Guide');
}
public function About(){
return view('FrontEnd.FrontWeb.about');
}
public function Download(){
return view('FrontEnd.FrontWeb.download');
}
public function ContectUs(){
return view('FrontEnd.FrontWeb.contact-us');
}
}
Here is My Web.php Route
<?php
use Illuminate\Support\Facades\Route;
use app\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Backend\AdminDashboardController;
use App\Http\Controllers\Backend\CategoryController;
use App\Http\Controllers\UserGuide\UserGuideController;
use App\Http\Controllers\Backend\AdminController;
use App\Http\Controllers\FrontEnd\IndexController;
// front end Route All GO Here
Route::get('/', function () {
return view('FrontEnd.FrontWeb.index');
});
Route::get('/User-Guide',[IndexController::class,'UserGuide'])->name('User.Guide');
Route::get('/about',[IndexController::class,'About'])->name('About.Page');
Route::get('/check-time-Software-download',[IndexController::class,'Download'])->name('Download.Page');
Route::get('/contact-us',[IndexController::class,'ContectUs'])->name('Contact.Us');
// Admin Route All Here
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('logout/',[AdminController::class,'logout'])->name('user.logout');
Namespaces are case-sensitive. In Laravel, the app namespace is with a lowercase a.
Method 1 :
Just import your controller in the routes file, like the following example, or use full path with controller file.
use App\Http\Controllers\IndexController;
Route::post('/about' , 'IndexController#About');
//OR
Route::get('/about', 'App\Http\Controllers\IndexController#About');
Method 2 :
Go to app/Providers/RouteServiceProvider.php and find and enable this line, as it should be commented.
protected $namespace = 'App\\Http\\Controllers';

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');

Class does not exist for laravel routes

Laravel 5.1
This seems strange to me:
Route::group([
'middleware'=>['auth','acl:view activity dashboard'],
'prefix' => 'api/v1'
], function(){
Route::controller('investment-transactions', 'Api\V1\Investments\InvestmentTransactionsController');
Route::controller('investment-transactions/{offeringID}', 'Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering');
});
Seems pretty normal to me, the controller:
namespace App\Http\Controllers\Api\V1\Investments;
use App\Brewster\Models\Company;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class InvestmentTransactionsController extends Controller {
public function __construct() {
}
public function getIndex() {
echo 'Here';
}
public function getTransactionsForOffering($offeringID) {
echo $offeringID;
}
}
Ok so the action and the controller do exit, but when I run: php artisan routes:list I get:
[ReflectionException]
Class App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering does not exist
Well obviously App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering is not a class, how ever: App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController is and getTransactionsForOffering is an action.
Whats going on?
I believe your problem is in the routes.php we can use controllers as follows
Route::get('investment-transactions', 'InvestmentTransactionsController#index');
Route::get('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
By default, our controllers are stored in App/http/controllers folder and laravel know it.
I believe you only need to reference the Class like so:
Route::controller('investment-transactions','InvestmentTransactionsController#Index'); //make sure you create a function for the index
Route::controller('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
Assuming you need to show a view for the route investment-transactions create the following function in your controller:
public function index()
{
return view('name-of-your-view-file');
}

Categories