I'm trying to run a native php script using laravel routes.php , view, controller. but no chance.
for example if i have a test.php inside views path and require it in a view blade:
<?php
require_once 'test.php';
?>
then build a route to that view and point to it:
FatalErrorException in b012cb264e405ddcdbcd54b1275905692fac6df9.php line 26:
main(): Failed opening required 'test.php' (include_path='.;E:\xampp\php\PEAR')
Try something like
require_once base_path('resources/views').'/test.php';
Or
This is in your config/view.php
require_once Config::get('view.paths').'/test.php';
Laravel 5+ (Global config)
require_once config('view.paths').'/test.php';
Related
I have a laravel project and in it, I create a regular (.php) file that I am posting some data to. In that file, I want to use a class that I installed using composer in the Laravel project. When I post data to the page I am getting the error No such file in directory and require(): Failed opening required 'vendor/autoload.php. How can I resolve this my project is almost complete if I can get this working.
I've tried: require "class name" but the same error.
PHP file posting to:
<?php
require "vendor/autoload.php";
require 'vendor\lcobucci\jwt\src\Builder.php';
require 'vendor\lcobucci\jwt\src\Signer.php';
//I've tried using these require statements
use \vendor\Lcobucci\JWT\Builder;
use \vendor\Lcobucci\JWT\Signer\Hmac\Sha256;
someFunctiion(){
// do stuff with function from class
}
?>
I expect that when I post to this page, the function from the class I am 'using' will be called but instead, it's saying require() failed to open class.
I'm trying to include .env file in Codeigniter 3 by following steps:
integrating.env files in CodeIgniter 3.0 using hooks
But, it returns the following error
A PHP Error was encountered Severity: 4096
Message: Argument 1 passed to Dotenv\Dotenv::__construct() must be an
instance of Dotenv\Loader, string given, called in
/application/config/hooks.php on line 5 and defined
Filename: src/Dotenv.php
Line Number: 31
My /application/config/hooks.php file is configured in the same way as indicated in the tutorial:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$hook['pre_system'] = function() {
$dotenv = new Dotenv\Dotenv(APPPATH);
$dotenv->load();
};
I had done a quick research and unable to find a perfect solution for the problem occurred, Can you guys please help me to find out a solution?
I was able to install phpdotenv in a different way from the instructions in the first tutorial.
In this tutorial installation is done without the use of Composer:
PHPDotenv for CodeIgniter (Installation without Composer)
If you try something like that :
$dotenv = Dotenv\Dotenv::create(__DIR__);
I assume the vendor folder is in project/vendor, not inside project/application/vendor folder. Then try this:
$hook['pre_system'] = function() {
$dotenv = Dotenv\Dotenv::create(FCPATH);
$dotenv->load();
};
I am using Alchemy API for filter out some data. Everything works fine in the native code. But when i used it in my Laravel Controller it throws Cannot redeclare class. My controller,alchemyapi.php and example.php are in the same directory. Here is how i include alchemyapi.php in native code
<?php
require_once 'alchemyapi.php';
$alchemyapi = new AlchemyAPI("MY API KEY"); ?>
But when i include it in the controller it throws my the error. is there something i am missing ?
require_once 'alchemyapi.php';
$alchemyapi = new AlchemyAPI("MY API KEY");
The native code(example.php) works well without any issue. But in laravel controller it throws a error saying Cannot redeclare class AlchemyAPI
in alchemyapi.php line 20
Instead of using require_once use namespace in your alchemyapi.php and then use use for same namespace in your MyController
alchemyapi.php
<?php
namespace App\Http\Controller;
class AlchemyApi {
//your code
}
MyController.php
<?php
namespace App\Http\Controller;
use App\Http\Controller\AlchemyApi;
class MyController {
$alchemy = new AlchemyApi("Your_api_key");
}
OK, so what I think is happening is that alchemyapi.php is somehow already being included by the composer autoloader.
Try this.
Create the directory lib in the root of your project.
Move alchemiapi.php into lib.
Under the "autoload" section in your composer.json add the following code. Make sure the JSON is valid:
"files": [
"lib/alchemyapi.php",
],
Run composer dump-autoload. If it errors, your composer.json is invalid or you haven't put the file in the correct place.
Delete require_once 'alchemyapi.php'; from your controller.
When dealing with composer, this is how you deal with classes in the global namespace. After running composer it scan those directories in app for PSR-4 classes.
I can't be sure but I think that composer is looking for it and you are also manually requiring it. That would explain why PHP thinks you are redeclaring the class.
I'm trying to setup a php client for RPC with a HBase Thrift server but I'm getting some errors at the begining ... I've compilated the HBase.thrift file (with the 0.9.1 compiler) and I've put the 2 generated files like bellow (/ is the root of my project, I'm using apache server on ArchOS) :
/include/Hbase.php
/include/Types.php
I've included the lib folder of thrift0.9.1/lib/php/lib (composed of Thrift and sub folders) under / and so I've the following file system :
/include/HBase.php
/Types.php
/Thrift/Base
/ClassLoader
/Exception
/ext
...
To be able to use my service (HBase.php) I've to load files so my code is the following :
<?php
$GLOBALS['THRIFT_ROOT'] = PHYSICAL_PATH;
// Load up all the thrift stuff
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/Transport/TTransport.php';
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/Transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/Protocol/TProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/Protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/Transport/TBufferedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/Type/TMessageType.php';
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/Factory/TStringFuncFactory.php';
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/StringFunc/TStringFunc.php';
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/StringFunc/Core.php';
require_once $GLOBALS['THRIFT_ROOT'].'Thrift/Type/TType.php';
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\TSocketPool;
use Thrift\Transport\TFramedTransport;
use Thrift\Transport\TBufferedTransport;
//hbase thrift
require_once ('Hbase.php');
require_once ('Types.php');
?>
When I'm charging the different files, I'm getting an error for this line --require_once ('Types.php');-- and the error is : Fatal error: Class 'Thrift\Exception\TException' not found in /var/www/html/POCThrift2/include/Types.php on line 1266
I understand it like : When I tried to include Types.php, the Thrift\Exception\TException namespace was not use (despite the use Thrift\Exception\TException; in the beginig of the Types file) but I don't know why I'm getting his error ... can you help me ? Does anyone has encountered this problem before ? Thx in advance, I'm stuuuck right now ...
I'm trying to use unirest, a new php lib for making rest calls.
I'd like to place it in a system-wide directory above my project. I then include it:
require_once ('../unirest-php-master/lib/Unirest/Unirest.php');
loads fine. Then I use it per the readme:
$response = Unirest::post(CSWA_URL ....
I get Fatal error: Class 'Unirest' not found in ...hello_world/sign_start.php on line 23
I then try to use the namespace (see the library's code. They use a Namespace Unirest statement before declaring the Unirest class.)
$response = Unirest\Unirest::post(CSWA_URL ....
I got further. Now: Fatal error: Class 'Unirest\HttpMethod' not found in ....unirest-php-master/lib/Unirest/Unirest.php on line 26 -- This is an error in the library code!
Q: Did I do something wrong? Did the authors of Unirest make a mistake? Do I have to place the library in ./lib? What's the best fix?
It looks like the Unirest code in Unirest.php relies on autoloading code from the two other files in the unirest lib directory (HttpMethod.php and HttpResponse.php).
The author suggests installing the package using composer, if you were to do that composer would add the Unirest namespace to the autoloader.php script it generates. From there you need to require the autoload.php file at the top of your script and it will handle loading classes that aren't defined.
Alternatively, if you don't want to use composer, I would just require the other two files in the unirest lib directory at the top of your script as well.