I want to parse an XML.
I writed this ResultController
<?php
namespace App\Http\Controllers;
use Auth;
use \App\User;
use Illuminate\Http\Request;
use XmlParser;
use Illuminate\Container\Container;
use Orchestra\Parser\Xml\Document;
use Orchestra\Parser\Xml\Reader;
class ResultController extends Controller
{
public function getResults()
{
$xml = XmlParser::load('http://www.xmlsoccer.com/FootballDataDemo.asmx/GetAllTeams?ApiKey=ZXRIQOWMCFARAWRQIMSLRXCTSZDOBNLOTYWXYXMZYGDSENFSRB');
$app = new Illuminate\Container\Container;
$document = new Orchestra\Parser\Xml\Document($app);
$reader = new Orchestra\Parser\Xml\Reader($document);
$xml = $reader->load('http://www.xmlsoccer.com/FootballDataDemo.asmx/GetAllTeams?ApiKey=ZXRIQOWMCFARAWRQIMSLRXCTSZDOBNLOTYWXYXMZYGDSENFSRB');
$user = $xml->parse([
'users' => ['uses' => 'Team[Team_Id,Name]'],
]);
// dd($xml);
return view ('results.live');
}
}
I used use Illuminate\Container\Container; at top of the controller but it gaves me this error:
FatalErrorException in ResultController.php line 13: Class
'Illuminate\Container\Container\Controller' not found.
I cant understand what is wrong with it?
If you've used use keyword above -
use Illuminate\Container\Container;
use Orchestra\Parser\Xml\Document as OrchestraDocument;
use Orchestra\Parser\Xml\Reader as OrchestraReader;
you should use it inside method as (updated):
$app = new Container;
$document = new OrchestraDocument($app);
$reader = new OrchestraReader($document);
As you've used new Illuminate\Container\Container the php would find
your container as -
App\Http\Controllers\Illuminate\Container\Container, which isn't the
correct path, the use keyword helps php to recognize the namespace
of class Container
For more info see PHP Namespacing Docs
Hope this helps!
Related
I have a CRUD app, everything works except updating tags
Here is the update function in my controller
namespace App\Http\Controllers;
use App\Tag;
use App\PageList;
use App\PageListTag;
use Illuminate\Http\Request;
public function update(Request $request, $id)
{
$pages = PageList::find($id);
$pages->pagetitle = $request->get('pagetitle');
$pages->articlelist = $request->get('articlelist');
$pages->status = $request->get('status');
$pages->save();
$pages->tags()->saveMany([
new App\Tag(),
new App\Tag(),
]);
return redirect('/pages')->with('success', 'pages updated!');
}
Here is the Tag model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $fillable = ['page_list_id', 'page_list_tag_id'];
protected $with = ['tag'];
public function tag()
{
return $this->belongsTo('App\PageListTag', 'page_list_tag_id', 'id');
}
}
When I run my app I am getting the following error
Class 'App\Http\Controllers\App\Tag' not found
What am I doing wrong in my code?
You're resolving in the wrong way the models namespaces. Please have a look at the official PHP documentation
In your code you're resolving the Tag class as follows
use App\Tag; // <-- This is right
But in your method you're calling
$pages->tags()->saveMany([
new App\Tag(), // <-- And this is wrong!
new App\Tag(),
]);
You simply have to call new Tag() since the use at the top of your file has already included the class.
Otherwise PHP will try to resolve the class from the current namespace. That's why it's throwing
Class 'App\Http\Controllers\App\Tag' not found
To be right you should have added a \ before App\Tag, so PHP will resolve the class from the root. In this case, the use statement will be useless
Your namespace is App\Http\Controllers, so when you create a tag with the syntax new App\Tag() it is indeed translated into App\Http\Controllers\App\Tag.
So just replace your instructions new App\Tag() with new Tag().
Alternatively, you could also use the absolute notation:
new \App\Tag()
I'm starting Laravel and i'm trying to create an external class to use like a 'Library'.
I've searched a lot and came up with this solution :
I created a folder 'Services' in 'App' and made a class file like so :
App/Services/OvhApiHandlerClass.php
This file looks like so :
<?
namespace App\Services;
use App\OvhBill;
use App\OvhBillDetail;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Ovh\Api;
class OvhApiHandlerClass
{
public function ovh_get_bills(string $consumer_key, $from = null, $to = null)
{
// Do something
}
}
So now i want to use this class inside a controller.
But i'm getting an error saying my class does not exist.
Here is my Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Services\OvhApiHandlerClass;
class OperationsController extends Controller
{
public function index()
{
$from = new \Carbon\Carbon('first day of January 2019');
$ovhHandler = new OvhApiHandlerClass();
$ovhHandler->ovh_get_bills('K87u3410p89ijKLao', $from);
return view('operations.index');
}
}
I already did, of course
composer dump-autoload
I'm kinda lost, what am i missing?
Thank you very much for your time !
I tried to use jasonmapper just as written in manual.
I required autoload.php file, and when construct JasonMapper object, I go class not found exception.
(1/1) FatalThrowableError
Class 'App\Http\Controllers\JsonMapper' not found
Here is my code
namespace App\Http\Controllers;
require __dir__.'/../../../vendor/autoload.php';
use Illuminate\Http\Request;
use App\Http\Games\Numbers;
class ApiController extends Controller
{
public function home()
{
$client = new \GuzzleHttp\Client();
$res = $client->request(
'GET',
$testurl
);
$json = json_decode($res->getBody());
$mapper = new JsonMapper();// error occurs at this line
$numbers = $mapper->map($json, new Numbers());
return json_encode($numbers);
}
}
If you don't "use" JsonMapper at the top of your script, PHP assumes that JsonMapper is in the App\Http\Controllers namespace, which it's not. That means in your script you must:
$mapper = new \JsonMapper();
// src/Acme/DemoBundle/Tests/Controller/DemoControllerTest.php
namespace Acme\DemoBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DemoControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/demo/hello/Fabien');
$this->assertGreaterThan(0, $crawler->filter('html:contains("Hello Fabien")')->count());
}
}
this working OK in my tests, but i would like use this crawler also in controller. How can i do it?
i make route, and add to controller:
<?php
// src/Ens/JobeetBundle/Controller/CategoryController
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\DemoBundle\Entity\Category;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class CategoryController extends Controller
{
public function testAction()
{
$client = WebTestCase::createClient();
$crawler = $client->request('GET', '/category/index');
}
}
but this return me error:
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /acme/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php on line 24
The WebTestCase class is a special class that is designed to be run within a test framework (PHPUnit) and you cannot use it in your controller.
But you can create a HTTPKernel client like this:
use Symfony\Component\HttpKernel\Client;
...
public function testAction()
{
$client = new Client($this->get('kernel'));
$crawler = $client->request('GET', '/category/index');
}
Note that you will only be able to use this client to browse your own symfony application. If you want to browse an external server you will need to use another client like goutte.
The crawler created here is the same crawler returned by WebTestCase so you can follow the same examples detailed in the symfony testing documentation
If you need more information, here is the documentation for the crawler component and here is the class reference
You shouldn't use WebTestCase in prod environment, because WebTestCase::createClient() creates test client.
In your controller you should do something like this (I recommend you to use Buzz\Browser):
use Symfony\Component\DomCrawler\Crawler;
use Buzz\Browser;
...
$browser = new Browser();
$crawler = new Crawler();
$response = $browser->get('/category/index');
$content = $response->getContent();
$crawler->addContent($content);
My application file:
<?php // /src/app.php
require_once __DIR__ . '/../lib/vendor/Sensio/silex.phar';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Foo\Bar;
$app = new Silex\Application();
$app['autoloader']->registerNamespace('Foo', __DIR__);
$bar = new Bar();
(...)
My Bar class:
<?php /src/Bar.php
namespace Foo;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Response;
class Bar implements ControllerProviderInterface { ... }
When I do a $bar = new Bar() in my app.php, I get an error: Fatal error: Class 'Moken\Classname' not found in (...)/src/app.php on line 11
Can anyone tell me what I am doing wrong?
If you use namespace Foo; you must locate this class in Foo directory
Every namespace part is a directory in symfony
If not works, you must show the loader where to find this class
In symfony2 I use for this:
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
// HERE LOCATED FRAMEWORK SPECIFIED PATHS
// app namespaces
'Foo' => __DIR__ . '/../src',
));
In your main php file (index.php) you must:
declare the use of your Controller Provider;
after creation of your Application object you must register your namespace;
mount your Controller Provider.
For example (Example\Controllers is the namespace and XyzControllerProvider is the Controller Provider, the url is /my/example):
[...]
// declare the use of your Controller Provider
use Example\Controllers\XyzControllerProvider;
[...]
//after creation of your Application object you must register your namespace;
$app = Application();
$app['autoloader']->registerNamespace('Example', __DIR__.'/src');
[...]
//mount your Controller Provider
$app->mount('/my/example', new Example\Controllers\XyzControllerProvider());
the Controller Provider (under src/example/controllers) will be:
<?php
namespace Example\Controllers;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
class XyzControllerProvider implements ControllerProviderInterface {
public function connect(Application $app) {
$controllers = new ControllerCollection();
$controllers->get('/', function (Application $app) {
return "DONE;"
});
return $controllers;
}
}