Symfony: getting Class not found error - php

I am trying to learn Symfony 2. I'm trying to follow the example in the Symfony book (Page 17). I have downloaded and unpacked Symfony in my working directory.
The absolute path to the file I'm trying to use is in: symfony\vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\Request.php
However following the books example closely, I just put:
use Symfony\Component\HttpFoundation\Request
Either way, both these approaches did not work for me.
Using the books approach I'm getting the following error:
Fatal error: Class 'Symfony\Component\HttpFoundation\Request' not found in C:/...
Using my approach with the longer path, I'm still getting this error, when I use include to navigate to the long path, the file is found but I'm getting some other error (just wanted to verify I'm able to get to the file, which I am).
I appreciate any advice in overcoming this problem. Many thanks in advance!

in PHP use statement requires Fully Qualified Class Name, not directory. So use Symfony\Component\HttpFoundation\Request is just fine.
If you decided to try these "examples" you will have to require autoload.php file first. It is located in your vendors directory.
So if you make file named "myfile" in symfony web folder it should start like this:
<?php
require '../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();

Run - > composer update
Run the Path -> ./vendor/bin/upgrade-carbon
Then Will fixed this issue in laravel

Related

Is there no manual include of Laravel?

I was starting some .php files in the /public directory of Laravel, which works, naturally, but it is separated from the standard Laravel system. In fact, Laravel wants you to you routes I know, and if I want to use some Laravel stuff I would think that calling
require_once 'path/to/vendor/autoload.php';
require_once 'path/to/app/Services/Myfile.php';
would let one use that... but for example "use GuzzleHttp", if used in myfile, gets fatal "Uncaught ReflectionException: Class config does not exist" in Container.php of Laravel.
I know there's SHORTINIT in Wordpress though it is kind of a mess of requiring any file that you need and all the files with functions it uses... Is there something similar for Laravel? Or this is never properly used this way to hold php files within /public?
You don't need to use require as composer takes care of loading the library files.
However when you add any new library or make any changes to routes or config files make sure you run the optimize command.
php artisan optimize

registering Bundle to symfony kernel

Instead of Using composer I download zip file of a bundle and extract this bundle to my symfony project. I change all relative address for example I change all namespace Trsteel\CkeditorBundle; to namespace Acme\TrsteelCkeditorBundle; for doing this I searched the downloaded directory for every Trsteel\CkeditorBundle and change all of them to Acme\TrsteelCkeditorBundle now when I want to use this Bundle on my project I register this bundle to my appKernel.php of symfony project But I reached this error
Error: Class 'Acme\TrsteelCkeditorBundle\TrsteelCkeditorBundle' not found in /Users/kingkong/Documents/workspace/dev/Project/SRC/app/AppKernel.php line 45
File names should be the same as namespace structure, capitalization may be an issue, so check that.
You may want to place the bundle in this way - "Acme\Trsteel\CkeditorBundle\CkeditorBundle"
Check whether you have actually changed the folder structure from Trsteel\CkeditorBundle to Acme\TrsteelCkeditorBundle. And also check your routing files as well. (I assume you have done the routing part as the error won't say that it can't get to that location.)
Cheers!

Laravel: How to include file from Vendor folder in Laravel

I am trying to include the YouTube Analytics Service of Google but I can not access it through the Vendor folder.
include(app_path.'path/to/analytics/Google_YoutubeAnalyticsService.php')
It is not working, because it defaults to the App folder.
How can I get out of the App folder and into the Vendor folder (where the YouTube Analytics file is at)?
The error is {
include(C:\xampp\htdocs\mysite\app/path/to/analytics/Google_YoutubeAnalyticsService.php):
failed to open stream: No such file or directory
From where do you want to include that file ?
Place a reference to your file in composer.json autoload object:
"autoload": {
"files":["your_file_path"]
}
Run composer dumpautoload, and you'll have your file :)
Actually you have in the helpers function the path so basically the function base_path give the direction to the root of your project so
echo base_path() . '/vendor';
Should be the route to your vendor folder.
You can se all the documentation in
Helper Functions Laravel
Be sure that you are seeing the documentation of the laravel version that you are using (I put the link for the 4.2 version).
This question was asked a long time ago and the answers reflect that. Most the time now all you need to do is import it using the "use" statement if you installed it with composer. Composer will already reference all the important directories.
It should be something like this, but it will vary depending on the project.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\Class;
That could include a base class as well as some exception classes.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\ClassException;
Usually most packages if compliant with modern composer and php standards work in this fashion.

Using pre existing library in Symfony

I am new to Symfony. I am trying to use a pre existing library that I have used for my Payment Gateway API, with Symfony (v2.3).
Before using Symfony I would have a composer.json file in the root directory and I would simply use PHP require 'vendor/Braintree.... However with Symfony I am having a really difficult time using the library for payment gateway API by importing it to my Controller.
Note: I am using an Entity in the same controller as follows, which has a similar directory structure and works great:
use Jets\JetsBundle\Entity\Company;
This is what I tried to use the payment gateway API:
use Jets\JetsBundle\Braintree\braintree\braintree_php\lib\Braintree;
and the Braintree.php contains:
namespace Widb\WidbBundle\Braintree\braintree\braintree_php\lib;
I keep getting the following error:
FatalErrorException: Error: Class 'Jets\JetsBundle\Controller\Braintree_Configuration' not found in C:\xampp\htdocs\www\symfony\src\Jets\JetsBundle\Controller\DefaultController.php line 239
And the DefaultController.php Line 239 contains:
Braintree_Configuration::environment('sandbox');
As a side note, I didn't do anything other than drag / drop the ready configured library directory from my old server to the Symfony directory on the new server. Am I missing some configuration script or cmd set up or something?
I appreciate any assistance in this regard. I will forever be grateful is someone can help me troubleshoot this.
Here is my DefaultController.php code:
http://pastebin.com/kwisEBzL
Many thanks in advance!
This Braintree project seems to be PSR-0, so you should be able to use it pretty easily.
Don't do this:
use Jets\JetsBundle\Braintree\braintree\braintree_php\lib\Braintree;
There is no such namespace.
Include the Braintree project using Composer (notice they have the PSR-0 autoloading config already: https://github.com/braintree/braintree_php/blob/master/composer.json).
EDIT: add this to your composer.json and don't forget to run composer update (or php composer.phar update)
Update 2: As pointed out in the comments, it is a good idea to use a stable tag/branch. That way, if there are breaking changes, you can make sure to fix them before updating. So always check the version of the library (2.23 may not be the latest version any more).
"require": {
...
"braintree/braintree_php": "~2.23"
},
The autoloader should pickup all of the classes if you use the global namespace (notice the leading slash):
\Braintree_Configuration::environment('sandbox');
Let me know if this works.
Side note, you should probably create a bundle that does the configuration for you in a centralized place.
You really missed out one of the first comments.. which was spot on and will most likely take away alot of headaches.
Follow the instructions given at https://github.com/cometcult/CometCultBraintreeBundle, this is a bundle that provides "Braintree". A bundle in this case actually is a "module" you can hook in, and configure by configuration files.
Relevant commands that are missing for handling composer:
http://getcomposer.org/doc/03-cli.md#install
http://getcomposer.org/doc/03-cli.md#require
As soon as you have finished it up you should take a look at the bottom two parts, besides conifgurating everything.
$factory = $this->get('comet_cult_braintree.factory');
$customerService = $factory->get('customer');
If you need more help let me know
I'm don't uderstand what you actually try to do in your DefaultController.php, but I have some assumptions.
UPD:
FIRST and SECOND point was deleted.
THIRD:
When you trying to make use Jets\JetsBundle\Braintree\braintree\braintree_php\lib\Braintree; you have load only class Braintree from file Braintree.php. Not whole file with another data like require_once instructions.
FOURTH:
/vendor directory of Symfony2 projects usually contains third-part libs. So you can put your lib here.
Hope some of this points helps you.
P.S. Sorry for poor English.

Error with propel-generate-crud in Symfony 1.0

When I try to generate a CRUD test for a new project I am getting a PHP Warning and a Fatal Error.
The errors relate to files that it cannot find, however, I have checked and the files are definitely there.
The error text is 'require_once(lib/model/map/QuestionMapBuilder.php): failed to open stream: No such file or directory in c:\webroot\askeet\lib\model\om\BaseQuestionPeer.php on line 547'
What details of my project should I check?
I think it's a problem with your include path.
Check it, the require_once() call is looking for lib/model/map/QuestionMapBuilder.php
But your include_path is C:\webroot\askeet\lib
Which, when resolved together into a full path, would look like this
C:\webroot\askeet\lib\lib\model\map\QuestionMapBuilder.php
So, I think your solution is to add C:\webroot\askeet to your include path.
You are generating crud for the Question model class but it doesn't seem to exist. Documentation on using the crud generator
First you must use the schema.yml file to define your database, and run
./symfony propel:build-model
to generate your model files. (this will generate lib\model\map\QuestionMapBuilder.php)
I finally tracked down the issue. In my PHP.ini files, they were setup wit the default UNIX file path settings.
Weirdly nothing had ever been broken by this incorrect configuration. Plus, everything in Symfony had worked up until now.
I have switched to Windows style file_path and all is well again.
Thanks for all the answers!

Categories