include external php files(not in laravel directory) in laravel - php

I want to include an external PHP file into my service provider, that file is in a different folder.
Like my file is in folder1 and this folder is at same level as laravel is.
C:\xampp\htdocs\registration\php\file.php //this is file
C:\xampp\htdocs\_problem_sharing\app\AppServiceProvider
This is how I am trying right now
include_once "/../../../registration/php/user_info.php";

Is really simple to do this. Because everything in Laravel 5 is autoloaded using PSR-4, within the app/ directory. So, for example, if this file you want to include have a class.
You need to create the directory, e. g.: app/CustomStuff/CustomDirectory/
Into this directory, create the file: app/CustomStuff/CustomDirectory/SomeClass.php
Into the SomeClass.php file, you just need:
<?php
namespace App\CustomStuff\CustomDirectory;
class Someclass {}
Now, you can access this class using the namespace within your classes:
use App\CustomStuff\CustomDirectory\SomeClass;

Related

Yii2 - extension structures

Recently I added a telegram bot extension in my Yii2 application to use it. but actually it is not a Yii2 extension but its a normal php namespace structured files and classes.
the name of this telegram extension is irazasyed/telegram-bot-sdk actualy its the name that added to my composer.json. I want to know how can I make some classess like this extension?
the irazasyed/telegram-bot-sdk structure is like this:
vandor >>
irazasyed
telegram-bot-sdk
composer.json
license
src
class1.php
class2.php
and the class files can be accessed from the namespace like \Telegram\Bot\Api from any controller in my application.
i want to know how can I make something like this myself.
I want this structure:
vendor >>
myCustomName
myCustomPakageName
composer.json
license
src
Class1.php
Class2.php
and access the class files from this namespace \something\somethingElse\Class1;
how can I do this?
Just add your files in a folder (or folders) in the project folder and use suggested root namespace. You don't need composer.json since you don't need to install it using composer. And you should not put it in vendor folder.
Example for basic project template:
- put everything in myCustomName folder in application root folder,
- set namespaces for each class like app\myCustomName (+ whatever subfolder you are using)
Example for advanced project template:
- put everything in myCustomName folder in selected application root folder (like frontend or common),
- set namespaces for each class like frontend\myCustomName (or common instead of frontend or whatever + whatever subfolder you are using)

According to this tutorial where have I to create this class in my Laravel project (a class implementing the UserProviderInterface interface)

I am abslolutly new in PHP and moreover in Laravel framework (I came from Java).
I am following this tutorial to create a custom authentication driver:
http://laravel-recipes.com/recipes/115/using-your-own-authentication-driver
I have a very newbye doubt: at the beginning of this tutorial it show that I have to create a class that implements UserProviderInterface.
It only show the code but not where this class have to be put into my Laravel project. The only clue about its positioning is the namespace:
namespace MyApp\Extensions;
But exactly where have I to put it?
I have the following structure:
It say to put it into MyApp\Extensions but I have not MyApp and Extension folder in my project? (or maybe the nampespaces name doesn't reflect a directory structure into the project tree?)
So where have I to create this class?
if you want it to go inside MyApp\Extensions, consider MyApp as the app folder.
Then only thing to do is create a folder named Extensions inside app folder and create your UserProviderInterface.php there.
But If I were you, I'd create it under app\Auth\Providers\UserProviderInterface.php
I believe what the page author meant was to create a folder named Extensions and create the provider file under app/Extensions folder. MyApp is just a custom namespace that the author chose, that in default laravel app, it should be App.
Which means, if you create a folder Extensions under app folder, the DummyAuthProvider should then be in the namespace of namespace App\Extensions;

Yii2 namespace autoloader doesn't find copied files

I am running the Yii2 framework locally, and I want to reuse an model I created in a earlier project.
So I copy the file TestForm.php to the models directory, change the namespaces from namespace backend\models to namespace app\models and try to create an object from it with:
$model = new \app\models\TestForm;
Which gives me
Unable to find 'app\models\TestForm' in file: /var/www/html/operators/basic/models/TestForm.php. Namespace missing?
Which is weird because the namespace is correct.
However, if I create the file TestForm.php myself and copy the contents of the older file over, everything works fine.
What's going on?
(I use ubuntu 15.04)
I think in your /models/TestForm.php you don't have specified the correct namespace eg :
namespace basic\models;

Cakephp 3 loading vendor files

In cakephp 2 when I need a vendor or related class to be loaded globally, i was adding require or app use inside bootstrap.php ot core php.
In cakephp 3 where should I require vendor files ? I dont want to declare vendor require in every class and template file that I use my vendor files.
http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files
Vendor files are 3rdparty files. You custom static utility classes are not vendor files but rather your app files. You can put them under src/Lib/. Just ensure to use proper namespace for the classes and add proper use statement wherever you need to use your class.
For e.g. if your lib class is src/Lib/FooBar.php then it should have classname App\Lib and the "use" statement would be use App\Lib\FooBar.

How to include files in PHP Phalcon controllers

my file structure for phalcon is something like this
--app
-----controllers
-----models
-----views
-----functions
-----libraries
where functions and libraries folder contains some third party scripts like facebook log in etc, when I include the script in my controller classes, I did
include "../functions/facebook.php"
but it gives a file not found error.
How should I include files in the controller classes?
Ideally you should be using an autoloader Phalcon provides for automatically loading classes.
For non-classes, did you try using full path?
include __DIR__ . "/../functions/facebook.php";

Categories