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;
.........
........
}
}
Related
I'm trying to include a library/plugin named mPDF. Here is my code:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class HomeController extends Controller {
public function cert() {
require_once base_path('App/Http/Controllers/mpdf/mpdf.php');
$mpdf=new mpdf\mPDF();
return true;
}
}
I keep getting the error:
Cannot declare class mPDF, because the name is already in use
When I comment out the require_once line, I get:
Class 'App\Http\Controllers\mpdf\mPDF' not found
mpdf.php has a class named mPDF. It only has one class with this name. This class appears nowhere else in my project. No other classes have the same name.
Update the name space of your
App/Http/Controllers/mpdf/mpdf.php file
put something on top of the file like :
namespace App\Http\Controllers\MyPDF;
After defining name space in your library class file App/Http/Controllers/mpdf/mpdf.php file, include that class into your controller as
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Controllers\MyPDF\mPDF;
class HomeController extends Controller {
public function cert() {
//require_once base_path('App/Http/Controllers/mpdf/mpdf.php');
$mpdf=new mPDF();
return true;
}
}
I would recommend not to create library class as a controller or inside controller directory, create library classes in /app/ directory and provide your own namespace.
Actually Namespaces are same as package name in JAVA, you just need to define proper name space for your class and include your class without any conflict if there are same classes. Same class name and different name space will not cause conflict.
Within App/Http/Controllers/mpdf/mpdf.php file make sure you have the namespace.
namespace App\Http\Controllers\mpdf;
Then change your code to:
namespace App\Http\Controllers;
class HomeController extends Controller {
public function cert() {
$mpdf=new mpdf\mPDF();
return true;
}
}
The use use App\Http\Controllers's within the above code are not required as you're already in the namespace.
I encountered the topic called 'namespace' in php after I started working with Laravel. While trying to understand namespace I found that to extend a class under a namespace, I need to include that class in my current page. Like the following:
directory '..\teacher\Teacher.php'
namespace Teacher;
class Teacher{
public $headTeacher='mr X';
}
to extend the calss i need to include that page as well as use the namespace
directory '..\studnet\student.php'
use \Teacher\Teacher; //use the namespace
include('../teacher/Teacher.php'); // include the page
class mathTeacher extends Teacher{
public function headTeacherName(){
echo $this->headTeacher;
}
}
$student=new mathTeacher();
$student->headTeacherName();
I am wondering how Laravel only use namespace to include classes. Like if I create a controller called 'userController'. The structure of the page is
namespace App\Http\Controllers;
class userController extends Controller{
}
They never included the php page which holds the 'controller' class. But they were able to extend it somehow. Also I can use "View" ,"Auth" just by using the use View or use Auth command. How is it done? How can I implement the same with the code I have provided? Thanks in advance.
Laravel uses composer.php for autoloading the classes. All classes in the autoload directory will be pre loaded. So you can just use the namespace and consume anywhere across the application.
Learn more about composer, composer config can be found on composer.json in your root path for the application
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.
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'm new to Laravel and using PHP namespaces in general. I didn't run into any problems until I decided to make a model named File. How would I go about namespacing correctly so I can use my File model class?
The files are app/controllers/FilesController.php and app/models/File.php. I am trying to make a new File in FilesController.php.
Namespacing is pretty easy once you get that hang of it.
Take the following example:
app/models/File.php
namespace App\Models;
class File {
public function someMethodThatGetsFiles()
{
}
}
app/controllers/FileController.php
namespace App\Controllers;
use App\Models\File;
class FileController {
public function someMethod()
{
$file = new File();
}
}
Declare the Namespace:
namespace App\Controllers;
Remember, once you've put a class in a Namespace to access any of PHP's built in classes you need to call them from the Root Namespace. e.g: $stdClass = new stdClass(); will become $stdClass = new \stdClass(); (see the \)
"Import" other Namespaces:
use App\Models\File;
This Allows you to then use the File class without the Namespace prefix.
Alternatively you can just call:
$file = new App\Models\File();
But it's best practice to put it at the top in a use statement as you can then see all the file's dependencies without having to scan the code.
Once that's done you need to them run composer dump-autoload to update Composer's autoload function to take into account your newly added Classes.
Remember, if you want to access the FileController via a URL then you'll need to define a route and specify the full namespace like so:
Route::get('file', 'App\\Controllers\\FileController#someMethod');
Which will direct all GET /file requests to the controller's someMethod()
Take a look at the PHP documentation on Namespaces and Nettut's is always a good resource with this article
first, load your class with:
$ composer dump-autoload
then
$file = new File;
// your stuff like:
$file->name = 'thename';
$file->active = true;
$file->save();
Section: Insert, Update, Delete on Laravel 4 Eloquent's doc
To namespace your model, at the top of your model class right after the opening
Then when you call from controllers you will call new Whatever\Model;
You probably have to do a dump-autoload with composer the first time around.
have a look to it.. hopefully will clear your query....
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\users;
class UserController extends Controller{
public function actionIndex()
{
echo "working on .....";
}
}
Namespaces are defined at the top of PHP classes right after the opening php script tag like this:
<?php
namespace MyNameSpace;
When you then want to use the namespaced class in some other class, you define it like this:
new MyNameSpace\PhpClass;
or import it at the top of the file (after namespaces if present) like this:
<?php
//namespace
use MyNameSpace\MyPHPClass;
//then later on the code you can instantiate the class normally
$myphpclass = new MyPHPClass();
In Laravel namespaces can be defined anywhere composer can autoload them, I'd recommend defining namespaces within the app directory. So you can define a namespace like Utils for holding Utility classes by creating a Utils directory in the app directory, creating our utility classes and defining the namespace as we did above.
Afterwards you have run the command to ask composer to autoload classes:
$ composer dump-autoload