how to access to laravel global Classes in packages - laravel

I am developing a Laravel package . In the packeges I need to File::delete for delete files, but The following error message is displayed :
Class 'Chee\Image\File' not found
Can you help me?

You have to declare it in the top of your class:
namespace Chee\Image;
use File;
class Whatever()
{
}
Instead of using the File Facade, you can also get it directly from the Laravel IoC container:
$app = app();
$app['files']->delete($path)
In a service provider, you can inject it as a dependency your package class:
class Provider extends ServiceProvider {
public function register()
{
$this->app['myclass'] = $this->app->share(function($app)
{
return new MyClass($app['files']);
});
}
}
And receive it in your class:
class MyClass {
private $fileSystem;
public function __construcy($fileSystem)
{
$this->fileSystem = $fileSystem;
}
public function doWhatever($file)
{
$this->fileSystem->delete($file)
}
}

You should be able to just use:
\File
Namespaces are relative to the namespace you declare for the class you are writing. Adding a "\" in front of a call to a class is saying that we want to look for this class in the root namespace which is just "\". The Laravel File class can be accessed this way because it is an alias that has an declared in the root namespace.

Assuming you have file something like that:
<?php
namespace Chee\Image;
class YourClass
{
public function method() {
File::delete('path');
}
}
you should add use directive:
<?php
namespace Chee\Image;
use Illuminate\Support\Facades\File;
class YourClass
{
public function method() {
File::delete('path');
}
}
Otherwise if you don't use PHP is looking for File class in your current namespace so it is looking for Chee\Image\File. You could look at How to use objects from other namespaces and how to import namespaces in PHP if you want

Related

How can I extend Laravel Dusk Browser?

I wish to override \Laravel\Dusk\Browser methods and extend it with my own, plus a few enhancements. Here is what I did so far but it gives me this error:
TypeError: Argument 1 passed to Tests\Browser\SequentialAppTest::Tests\Browser{closure}() must be an instance of Tests\MyBrowser, instance of Laravel\Dusk\Browser given,
called in
/var/www/gtest/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php on
line 67
/var/www/gtest/tests/Browser/SequentialAppTest.php:135
/var/www/gtest/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:67
/var/www/gtest/tests/Browser/SequentialAppTest.php:157
file: tests/MyBrowser.php
<?php
namespace Tests;
class MyBrowser extends \Laravel\Dusk\Browser
{
}
file: tests/Browser/SequentialAppTest.php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Tests\MyBrowser as Browser;
...
class SequentialAppTest extends DuskTestCase
{
....
}
Override newBrowser() in your test or in DuskTestCase:
use Tests\DuskTestCase;
use Tests\MyBrowser as Browser;
class SequentialAppTest extends DuskTestCase
{
protected function newBrowser($driver)
{
return new Browser($driver);
}
}

Laravel IoC doesn't automatically resolving the interface

I've just crossed this example, but it fails to resolve binding interface to implementation.
Having the follow code files:
// File: app/App/Services/Talkable.php
<?php
namespace App\Services;
interface Talkable {
public function talk();
}
// File: app/App/Services/Cat.php
<?php
namespace App\Services;
use App\Services\Talkable;
class Cat implements Talkable
{
public function talk()
{
return 'meow meow';
}
}
// File: app/Jobs/MakeSomeNoise.php
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Services\Talkable;
class MakeSomeNoise extends Job
{
private $talkable;
public function __construct(Talkable $talkable)
{
$this->talkable = $talkable;
}
public function handle()
{
return ($this->talkable->talk());
}
}
The binding are taken place in app/Providers/AppServiceProvider.php
// File: app/Providers/AppServiceProvider.php
...
$this->app->bind('App\\Services\\Talkable', 'App\\Services\\Cat');
The MakeSomeNoise job is dispatched from a Controller
// File: any controller
public function makeNoises()
{
return $this->dispatch(new MakeSomeNoise); // (*)
}
At the (*), I expect Laravel will automatically resolve the binding, but it doesn't. Here the error,
Argument 1 passed to App\Jobs\MakeSomeNoise::__construct() must be an instance of App\Services\Talkable, none given, called in ...
But if I just inject into controller constructor, it works fine.
Any thought on this?
My mistake in the code. The DI should be taken in handle() method, not constructor.
public function handle(Talkable $talkable) {
// blah lbah
}

Repository pattern Laravel 5

I am starting to learn about Laravel 5 repository pattern, and I can't get anything right! I probably used wrong namespace or 'use'; This is the error i am getting: Class App\Http\Controllers\ServiceController does not exist
This is my Controller:
namespace App\Http\Controllers;
namespace Repo;
use App\Http\Controllers\Controller;
use Repo\ServiceRepository;
class ServiceController extends Controller {
public function __construct(ServiceRepository $service){
$this->service = $service;
}
public function showServices()
{
$services = $this->service->getAllServices();
return view('services', compact('services'));
}
}
Here's the repository:
class ServiceRepository {
public function getAllServices(){
return \App\Service::all();
}
}
And here's my route:
Route::get('/services', 'ServiceController#showServices');
I've tried to change some namespaces but i end up with more errors :/
You have to read a bit about namespaces in PHP.
If your ServiceController is located in app/Http/Controllers directory, then the code has to be like follows:
<?php
namespace App\Http\Controllers;
use Repo\ServiceRepository;
class ServiceController extends Controller
{
public function __construct(ServiceRepository $service)
{
$this->service = $service;
}
public function showServices()
{
$services = $this->service->getAllServices();
return view('services', compact('services'));
}
}
Note that I removed namespace Repo and the first use statement.
A class has to have only a single namespace, so I removed the second Repo namespace.
Namespaces are really simple. In most cases namespace of a class just reflects a path to that class. In your case, it's App\Http\Controllers because your controller is probably located in app/Http/Controllers directory.
I've removed use App\Http\Controllers\Controller because your ServiceController and App\Http\Controllers\Controller are in the same directory, so they are in the same namespace. When two classes are in the same namespace, a full path with a namespace is not necessary for them to be able to access each other.
Hope that this helps.

How to "import" namespaces in Laravel?

I've built an API in Laravel, versioned as follows:
/controllers/api/v1/CommentController.php
/controllers/api/v2/CommentController.php
In my routes I call the correct controllers like so:
Route::resource('notification', 'api\v2\CommentController');
This works, but since I'm using namespaces in my controllers, I have to use the \ approach in order to find classes like \Response in the root namespace?
namespace api\v1;
class NotificationController extends \BaseController {
public function index()()
{
return \Response::json({});
}
}
Is there a way to avoid that backslash notation and using the right namespace?
I tried using use \App; but without result
U need to add "use Response;" to your code.
<?php namespace api\v1;
use Response;
class CommentController extends \BaseController {
public function index()
{
return Response::json('hello');
}
}

how to call model in laravel from controller

How to call a model in laravel.
My code is:
use Jacopo\Authentication\Models\Guide;
class SampleController extends BaseController
{
public function index()
{
$model='Guide';
$guide=$model::where('guide_link','=',"guide")->get();
print_r($guide);
}
}
This will produce Class 'Guide' not found error.
If you added your class you should run in terminal
composer dump-autoload
to update your class map. Otherwise autoloader may not "see" your class and you are getting this error.
You need to add the namespace to your string:
class SampleController extends BaseController
{
public function index()
{
$model='Jacopo\Authentication\Models\Guide';
$guide=$model::where('guide_link','=',"guide")->get();
print_r($guide);
}
}
You could also resolve it from the IoC container, but you need to register it first:
App::bind('Guide', 'Jacopo\Authentication\Models\Guide');
And then you should be able to:
$model = App::make('Guide');
$guide = $model::where('guide_link','=',"guide")->get();
But this is not a very good option

Resources