Previously I could do something like this:
/var/www/laravel/app/Http/Controllers/HelloController.php
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Hello\Hello as Hello;
class HelloController extends Controller {
public function index() {
$o = new Hello;
$o = new Hello('changed1','changed1');
var_dump($o);
}
}
/var/www/laravel/app/Models/Hello
<?php namespace App\Models\Hello;
use Illuminate\Database\Eloquent\Model\Hello.php;
class Hello extends Model
{
public function __construct($one='default1', $two='default2')
{
echo "First Param: $one","\n<br>\n";
echo "Second Param: $two","\n<br>\n";
echo "\n<br>\n";
}
}
routes.php
Route::get('tutorial', function() {
$app = app();
$app->make('Hello');
});
Instantiating without make works, but this now brings an error:
ReflectionException in Container.php line 776: Class Hello does not exist
What do you think I am missing in here?
Because your class has a namespace of App\Models\Hello, you must use the namespace of it when trying to instantiate the class. Therefore, your code should be like this:
Route::get('tutorial', function() {
$app = app();
$app->make('App\Models\Hello\Hello');
});
To expand a bit in the answer, you can remove the make() method call and just use the app() helper, and you'll end up with the same result:
Route::get('tutorial', function() {
$app = app();
$app->make('App\Models\Hello\Hello');
// you can achieve the same with the
// `app()` helper using less characters :)
app('App\Models\Hello\Hello');
});
Related
I have a file that i put in app\Classes\myVendor\dev_client_api.php. This file has a class in it:
class someClass{
//stuff
}
I want to use this class in a controller.
In my controller I have done the following:
namespace App\Classes\myVendor;
use dev_client_api;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}
When i execute this page I get:
Class 'App\Classes\myVendor\Controller' not found
I have to admit I am not sure what exactly I am doing. Any help would be great.
I assume your Controllers are in Laravel's default App\Http\Controller directory.
namespace App\Classes\myVendor;
class someClass {
//stuff
}
namespace App\Http\Controllers;
use App\Classes\myVendor\someClass;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}
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();
I try to create facade in laravel.
My Facade :
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class someclass_f extends Facade
{
protected static function getFacadeAccessor()
{
return new \App\Someclass();
}
}
My Base Class :
namespace App;
class Someclass
{
public function get($data = [])
{
echo "foo";
}
}
In Calling :
use App\Facades\someclass_f;
class my_class{
function(){
someclass_f::get();
}
}
I got error as :
Class 'App\Facades\someclass_f' not found
What's wrong with this ?
Any suggestions ?
Why do you call Someclass::get()? Your facade name is someclass_f.
Call it like: someclass_f::get(); and that will call get() function in \App\Someclass.
I have a interface dependency inject Question,
the error message look like can`t catch the class,
have any ideal?
Error Message:
FatalThrowableError in UserController.php line 27:
Class 'App\Http\Controllers\App' not found
My folder path :
UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Domain\Services\Social\SocialService;
use Domain\Services\Social\SocialInterface;
use Domain\Services\Social\Facebook;
class UserController extends Controller
{
public function fb(Request $request)
{
App::bind(SocialInterface::class, Facebook::class);
$target = App::make(SocialService::class);
return $target ->getCallbackData($request);
}
}
SocialService.php
namespace Domain\Services\Social;
class SocialService
{
public function getCallbackData(SocialInterface $social,$request)
{
return $social->getCallbackData($request);
}
}
Facebook.php
namespace Domain\Services\Social;
class Facebook implements SocialInterface
{
public function getCallbackData($request)
{
//Somethig inside...
}
}
public function fb(Request $request)
{
\App::bind(SocialInterface::class, Facebook::class);
$target = \App::make(SocialService::class);
return $target ->getCallbackData($request);
}
Try it with backslash.
I am having an issue getting a Facade to work properly with a dependency injected into the underlying class.
I have a class called 'Listing'. It has one dependency called 'AdvertRepository' which is an interface and a class called EloquentAdvert which implements the interface. The code for these three classes is here:
// PlaneSaleing\Providers\Listing.php
<?php namespace PlaneSaleing\Providers;
use PlaneSaleing\Repositories\Advert\AdvertRepository;
class Listing {
protected $advert;
public function __construct (AdvertRepository $advert_repository) {
$this->advert = $advert_repository;
}
public function test() {
$this->advert->test();
}
public function test2() {
echo "this has worked";
}
}
// PlaneSaleing\Repositories\Advert\AdvertRepository.php
<?php namespace PlaneSaleing\Repositories\Advert;
interface AdvertRepository {
public function test();
}
// PlaneSaleing\Repositories\Advert\EloquentAdvert.php;
<?php namespace PlaneSaleing\Repositories\Advert;
class EloquentAdvert implements AdvertRepository {
public function test() {
echo 'this has worked';
}
}
I have then created a service provider called ListingServiceProvider.php, which has the following code:
// PlaneSaleing/Providers/ListingServiceProvider.php
<?php namespace PlaneSaleing\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class ListingServiceProvider extends ServiceProvider {
public function register() {
App::bind('PlaneSaleing\Repositories\Advert\AdvertRepository', 'PlaneSaleing\Repositories\Advert\EloquentAdvert');
}
}
I also added this to the ServiceProviders array in app.php
Now, if I inject Listing as a dependency into a controller and call the test method (as shown below) Laravel correctly detects the dependency, instantiates EloquentAdvert via its binding and displays 'this has worked'.
// Controllers/TestController.php
use PlaneSaleing\Providers\Listing;
class TestController extends BaseController {
protected $listing;
public function __construct(Listing $listing) {
$this->listing = $listing;
}
public function test1() {
$this->listing->test();
}
}
Now, I then created a facade for Listing. I added a new facade as follows and added an alias in app.php:
// PlaneSaleing\Providers\ListingFacade.php
<?php namespace PlaneSaleing\Providers;
use Illuminate\Support\Facades\Facade;
class ListingFacade extends Facade {
protected static function getFacadeAccessor() {
return 'Listing';
}
}
I also added the following new lines to ListingServiceProvider.php:
<?php namespace PlaneSaleing\Providers;
use PlaneSaleing\Repositories\Advert\AdvertRepository;
use PlaneSaleing\Repositories\Advert\EloquentAdvert;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class ListingServiceProvider extends ServiceProvider {
public function register() {
App::bind('PlaneSaleing\Repositories\Advert\AdvertRepository', 'PlaneSaleing\Repositories\Advert\EloquentAdvert');
// New lines...
$this->app['Listing'] = $this->app->share(function() {
return new Listing(new AdvertRepository);
});
}
}
NOW...if I call Listing::test(), I get the following error: Cannot instantiate interface PlaneSaleing\Repositories\Advert\AdvertRepository.
If I call Listing::test2() , I get 'this has worked' so it seems the Facade is working correctly.
It seems that when accessing Listing via its Facade the binding between AdvertRepository and EloquentAdvert doesnt work. I have looked at my code in the ServiceProvider thinking it was the issue, but I cant figure it out.
Both the Facade and binding work when tested individually but not when both are used at the same time.
Any ideas???
OK, So I have figured it out...For those who run into a similar problem...
The offending statement was in ListingServiceProvider.php which read:
$this->app['Listing'] = $this->app->share(function() {
return new Listing(new AdvertRepository);
});
The error is the new AdvertRepository statement. The reason being is that, we are telling php to directly instantiate the interface 'AdvertRepository'. Instead, we need to tell Laravel to instantiate the appropriate implementation of the 'AdvertRepository' interface. To do that, we use App::make('AdvertRepository'). That way, Laravel uses the binding previously declared to instantiate the correct implementation.
If your constructor is not being inject with a class, you must tell Laravel what class will be used when it needs to instantiate a particular interface:
Put this in your filters or bindings file:
App::bind('PlaneSaleing\Repositories\Advert\AdvertRepository', function()
{
return new PlaneSaleing\Repositories\Advert\EloquentAdvert;
});