I have a custom class I added to Classes, invoice.php within the Classes folder (\App\Classes).
When using a controller function, I can't execute a static function, says Class not found. I have the namespace and use set up like any other classes, similar to Helpers.php.
Class:
<?php namespace App\Classes;
class invoice {
//
}
?>
Contoller:
<?php namespace App\Http\Controllers;
use App\Classes\invoice;
class CustomerController extends Controller {
//
}
?>
Error:
FatalErrorException in CustomerController.php line 284:
Class 'App\Http\Controllers\invoice' not found
I've been stuck here for two hours and I don't understand what I'm doing wrong. Composer is using psr-4 and the Helpers.php file works fine, but my custom class file doesn't.
Thanks
To follow PSR-4 you need to have your class names initial-caps. The "i" should be capitalized here:
<?php namespace App\Classes;
class Invoice {
//
}
Also fix your use as well. Then when Invoice is used in your controller, it will pick up the correct namespace to use for resolving that class. What's happening currently is it is not finding a matching reference in your use section, so it assumes it is in the same namespace of the controller class that calls it.
Related
I am using laravel-5.7. i am making multi auth system I'm getting the following error
Symfony\Component\Debug\Exception\FatalThrowableError Class '\App\Admin' not found
Suppose you have Model class called Admin in the App folder, then you are calling it somewhere in the controller.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model
{
//
}
Your controller will be
<?php
namespace App\Http\Controllers;
use App\Admin;
use Illuminate\Http\Request;
class YourController extends Controller
{
//Your code goes here
}
The answer is in the error that you get. You either don't have a class in your app directory named Admin or you don't have the namespace on top of your class.
<?php namespace App;
class Admin
{
}
or if your error is in a different class then you need to import it on top.
...
use PATH_TO_THE_ADMIN;
class YourClass {}
Most of the times when your class is not found, is basically as a result of not importing your namespace or typo error. so checke the two before anything else
I am new in laravel and importing a existing php site.
I created a controller named "List" then i need to create a object of a class, coded in a file which is been include by include_once() as shown,
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
$INCLUDE_ROOT = 'path/to/file';
include_once($INCLUDE_ROOT . "ServiceDetails.class.php");
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}
But i am getting an error like
Fatal error: Class 'App\Http\Controllers\ServiceDetails' not found
I dont have much idea of namespace "use" and "as".
May be that's why i am not able to solve this problem.
When creating a new object it is searching class in namespace location only, but it should also look in included files, i think.
If there's a namespace in current file, PHP will try to find class in current namespace and if it won't find it, you'll get fatal error. You should open ServiceDetails.class.php class to verify if there is namespace ...; at the beginning of the file (after <?php). If not you can simple add in your Lists file after:
use App\Http\Controllers\Controller;
the following line:
use ServiceDetails
and if it is, you should copy that namespace and add the following line:
use namespaceyoucopied\ServiceDetails;
of course in namespaceyoucopied place you need to put the correct copied namespace so it could look like this:
use A\B\C\ServiceDetails;
You can also look at How to use objects from other namespaces and how to import namespaces in PHP or PHP namespaces manual
You just need to add a use statement for that class so the class in the current file can "see" it.
<?php
namespace App\Http\Controllers;
$INCLUDE_ROOT = 'path/to/file';
include_once($INCLUDE_ROOT . "ServiceDetails.class.php");
use App\Http\Controllers\Controller;
use Namespace\To\ServiceDetails;
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}
However, if you are using Laravel and doing this, then you are not using the autoloading feature to its fullest. I recommend you put this file in a namespaced directory in your application and have it follow PSR-4. Then Laravel will load this for you, and it will keep your class file looking clean.
Put the file in a path like the following: /path/to/projectRoot/app/Lib/ServiceDetails.php. Then make the file look like below so it follows PSR-4:
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Lib\ServiceDetails;
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}
I have created a common class in app/Classes/Common.php
but whenever i try to access a model in a class function.
$new_booking_request = BookingRequest::where('host_id','=',Auth::id())
I am getting this error
Class 'App\Models\BookingRequest' not found
Even other classes like Auth, URL and Cookie are not working.
Is there a way to bring all classes in my Common class scope?
You get this issue when your namespace is wrong you or you forgot to namespace.
Since common.php is inside App/Classes, inside Common.php do somethng like this:
<?php namespace App\Classes;
use View, Auth, URL;
class Common {
//class methods
}
Also ensure your model class has the correct namespace, if BookingRequest.php is located inside App\Models then inside BookingRequest.php do this:
<?php namespace App\Models;
BookingRequest extends \Eloquent {
//other definitions
}
Then if you wish to use BookingRequest.php outside its namespace or in another namespace like so:
<?php namespace App\Classes;
use App\Models\BookingRequest;
use View, Auth, URL;
class Common {
//class methods
}
In Laravel 5 everything is namespaced, make sure your class has a proper namespace and that you are calling it using that same namespace you specified.
To include classes in another class make sure that you use the use keyword to import the necessary classes on top of your class definition. Also you can call the class globally with the \. Ex: \Auth, \URL and \Cookie
For the namespace in L5 here is a quick example:
<?php namespace App\Models;
class BookingRequest {
// class definition
}
then when trying to call that class, either call the full namespace path of the function, or include the function.
<?php
class HomeController extends Controller {
public function index()
{
$newBookingRequest = App\Models\BookingRequest::where('host_id','=',Auth::id());
}
}
OR
<?php namespace App\Controllers;
use App\Models\BookingRequest; // Include the class
class HomeController extends Controller {
public function index()
{
$newBookingRequest = BookingRequest::where('host_id','=',Auth::id());
}
}
PS:
Please use camelCase when defining class attributes and methods as this helps for a better code-styling and naming conventions when using the L5 framework.
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.
I have been searching websites to try and get a handle on using PHP namespaces, but they all seem quite vague but what they're trying to do is easy to understand!
My question is: I have a file called people.php and in it is defined class called people. If I create another file called managers.php in the same folder can I define a class again called people which extends the original people class but in the namespace of managers, if so do I have to 'include' the original people.php and if so do I put the include after the writing: namespace managers?
Namespaces are a way to group your related classes in packages. What you describe could best be put under a single namespace like
<?php // people.php
namespace com\example\johnslibrary\people;
abstract class People {
}
and then
<?php // manager.php
namespace com\example\johnslibrary\people;
require_once 'path/to/People.php'; // can better use autoloading though
class Manager extends People {
}
because a Manager is a subclass of People, so there is not much of a reason to put them into their own namespace. They are specialized People.
If you want to Managers to be in their own namespace, you can do so, but have to use the fully qualified name when using the extends keyword, e.g.
<?php // manager.php
namespace com\example\johnslibrary\managers;
require_once 'path/to/People.php';
class Manager extends \com\example\johnslibrary\people\People {
}
or import the People class first
<?php // manager.php
namespace com\example\johnslibrary\managers;
use com\example\johnslibrary\People as People;
require_once 'path/to/People.php';
class Manager extends People {
}
See the PHP Manual on Namespaces for extensive documentation.
// people.php
<?php
namespace People;
class People {}
// managers.php
<?php
namespace Managers;
require_once __DIR__.'/people.php';
class People extends \People\People {}
I have old PHP Class and i need to use it in new PHP file as for example:index.php has to use iClass.php. But before using the OLD iClass.php i have to modify it as below, so that i can use it in index.php.
iClass.php:
namespace ic;
class iClass {
public static function callMeFromClass() {
echo 'OK - you have called me!';
exit;
}
}
index.php
namespace inex;
require_once 'iClass.php';
use ic\iClass;
iClass::callMeFromClass();