Class Not Found - Laravel 5.1 - php

I have some issue with my controller. After I move the controller to sub folder it always tell me
Class 'Horsefly\Request' not found
I don't know what it is the problem. I did the same in admin sub folder and it worked fine.
Controller:
<?php
namespace Horsefly\Http\Controllers\includes;
use Illuminate\Http\Request;
use Horsefly\Http\Requests;
use Horsefly\Ebooks;
use Horsefly\Settings;
use Horsefly\Http\Controllers\Controller;
class EbooksController extends Controller {
}
Routes
get('/navpage', 'includes\EbooksController#navpage');
get('/content', 'includes\EbooksController#content');
get('/openModel', 'includes\EbooksController#openModel');
thanks for the help.

You declared use Horsefly\Http\Requests;
Just change it to
use Horsefly\Requests;
or use Request;

Related

Laravel Model not being found

Hi there I am having trouble using a model within a class there error being shown is Error
Class 'App\Models\RegisteredUsers' not found.
I have made sure that the namespaces match what is being used but I repeatedly get the same error.
Model code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class RegisteredUsers extends Model
{
//
}
Controller code
<?php
namespace App\Http\Controllers;
use App\Models\RegisteredUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class RegisterUser extends Controller
{
$UserObj = new RegisteredUsers();
}
The directory
App/Http/Controllers/RegisterUser - controller
App/Http/Models/RegisteredUser - model
I created the model and the controller using the command line with PHP artisan.
I have tried the solutions from
laravel model class not found
as well as Model not found in Laravel
and a few laracast questions but i still get the error.
May need to rebuild the classmap
composer dump-autoload
With App\Models namespace in your RegsiteredUser model, the RegisteredUser.php model file must be in the app/Models/RegisteredUser.php directory. Try to move the Models folder outside the Http folder. And from now, you should never put the Models folder in the Http folder again.
Your error is App/Http/Models/RegisteredUser and use App\Models\RegisteredUsers;, did you place your Models in Http?. that is not correct, the Models should be in App root directory, so you are calling the RegisteredUser in the wrong place

Can't find new controller created

I everyone I've created a new Controler inside my App\Controllers\Admin folder and I already have two files called AdminInquiriesController and AdminUsersController.
When I run my app it says that
Class App\Http\Controllers\AdminNewsController does not exist
I don't undersantd. In all my 3 files inside this folder I'm using the namespace namespace
App\Http\Controllers
if it's working for the others why is not working for this?
<?php
namespace App\Http\Controllers;
use App\Manager\InquiryManager;
use Auth;
use Illuminate\Http\Request;
use function GuzzleHttp\json_decode;
use App\Model\InquiryStatus;
use Carbon\Carbon;
use App\Manager\UserManager;
class AdminInquiryController extends Controller {
<?php
namespace App\Http\Controllers ;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
use App\Manager\NewsManager;
class AdminNewsController extends Controller {
In my route file..
// inquiries
Route::get('/admin/inquiries', 'AdminInquiryController#search');
// news
Route::get('/admin/news', 'AdminNewsController#search');
Route::post('/admin/news/new', 'AdminNewsController#create');
I know this is so silly but I'm not understanding what's happening...
You should to define namespace in your controller file;
namespace App\Http\Controllers\Admin ;
Also change your web.php route file to
Route::get('/admin/news', 'Admin\AdminNewsController#search');
Or you can define namespace in routes group by
Route::namespace('Admin')->group(function () {
Route::get('/admin/news', 'AdminNewsController#search');
}
Also you have to put
use App\Http\Controllers\Controller;
In your controllers files in Admin folder
Try to add a second forward slash when writing model location
something like this
--model=App\\Models\\ModelName

use a controller inside a folder in the controllers folder

I'm trying to use a controller inside a folder in the controllers directory like
route
Route::get('/','site\HomeController#index');
but seem's does not work like it gives me this error
Class App\Http\Controllers\site\HomeController does not exist
Note: I have also a HomeController.php in the controllers folder. I'm trying to organize my controllers by putting them unto their specific folders.
Any help, ideas please?
You should use proper namespace, like:
namespace App\Http\Controllers\Site;
And add this line:
use App\Http\Controllers\Controller;
Then this route will work:
Route::get('/','Site\HomeController#index');
The namespace of the class HomeController should be as:
namespace App\Http\Controllers\Site;
And in your route file you can use it as:
Route::get('/','Site\HomeController#index');
Remember to add following line of code in HomeController class as:
use App\Http\Controllers\Controller;

Laravel - Class/Model not found

Initially, this code worked, but it doesn't work anymore. I don't know what is causing the issue.
The error is :
FatalErrorException in AdminController.php line 64:
Class 'App\Category' not found
AdminController code is:
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Category;
use View;
class AdminController extends Controller
{
public function article_get()
{
$categories = Category::latest()->get();
return View::make('create.article')->with('categories',$categories);
}
}
My model Category is located at App/Models/Category.php.
What I've tried:
Change from use App\Category; to use Category, to use \Category, to use App\Models\Category.
composer dump-autoload.
A few hours ago I had a working project, but now my models are not found.
Because Laravel uses PSR-4 autoloading, you need to make sure your namespaces match the real directory structure.
You say that your Category model is located at App/Models/Category.php so its namespace should be App\Models. Then, in your controllers you would use App\Models\Category.
I had also faced the same error when Models are called inside Controllers or Seeders.
The best solution is to import your Model inside the Controllers.
Add the below line at the top of your AdminController.
use App\Models\Category
This is applicable for all Classes where you try to call your models.
Do it like this and it must work:
use App\Models\Category
and make sure that the model Category is inside a folder named Models.
Your code seems fine.
May be it has an issue with namespace in Category model.
if you use this code for Category Model in your AdminController controller:
use App\Models\Category;
then your Category model itself has this namespace
namespace App\Models;
Check if your model namespace is still there or not.
Thanks
App\Models\Category
PLease Check This twoThings
1.Importing namespace App\Http\Controllers;
2,Check Controller Letter is in Upper Or LowerCase
like this
use App\models\Category----->❌❌❌
use App\Models\Category----->✅✅✅
use App\Models\Category
//check your models is capital or small letter sometimes issue like that is a big problem

phpcs - class must be in a namespace of at least one level - how to fix?

I am using laravel 4.2.
Lets say there is such class:
class BShopsController extends ShopsController
To fix this, I try to use name space lets say this:
namespace app\controllers;
and then it does not find ShopsController
so I add
use \ShopsController;
Then I get error:
Class BShopsController does not exist
What namespace should I use first of all so it would not break anything?
Edit:
BShopsController and ShopsController are in folder Shops
As your files are inside the Shops folder and I believe that the Shops folder is inside the app folder you should namespace your class the following way.
<?php namespace Shops;
class BShopsController extends ShopsController{}
Similarly,
<?php namespace Shops;
class ShopsController{}
So with the help of Shhetri and this Using namespaces in Laravel 4
I did this way:
namespace App\Controllers\Shops;
class BShopsController extends ShopsController{}
Also in routes.php then need to change to this:
Route::controller('shops', 'App\Controllers\Shops\ShopsController');
And where calling action() method - also need to use namespace.
Also needed to run
composer dump-autoload -o
otherwise were errors.
Also in ShopsContrller needed to to this:
use \App\Controllers\BaseController;
Because Shops controller was in another namespace than BaseController and cannot find it. But is extending from BaseController, so need it.

Categories