Laravel 4 interface file error: "syntax error, unexpected 'interface' (T_INTERFACE)" - laravel

When trying to do any actions that invoke my Repo class, I get the following error on the child Repo interface:
"syntax error, unexpected 'interface' (T_INTERFACE)"
Here is the code for the Repo class, and the child repo interface file that is the source of the error:
<?php namespace MyProj\Repository\Parent;
use MyProj\Parent;
use MyProj\Parent\ChildRsrc as ChildRsrc;
class EloquentParentRepo implements ParentRepoIfc
{
public $childRepo;
public function __construct(ChildRsrcRepoIfc $childRepo)
{
$this->childRepo = $childRepo;
}
// ...
}
and the child repo interface file:
<?php namespace MyProj\Repository\Parent;
interface ChildRscrRepoIfc // the error info points to this line
{
public function all();
public function create($input);
public function delete($id);
public function find($id);
}
It all is structured just like other code in the project that works fine. The ChildRsrcRepoIfc and its concrete implementation (EloquentChildRscrRepo) are in the same Parent namespace as everything else referenced here. I've triple-checked all spelling for typos. Any help is appreciated, as always.
Update: I tried removing the DI in the parent class and using direct instantiation but still got the same error:
class EloquentParentRepo implements ParentRepoIfc
{
public function __construct()
{
$this->childRepo = new ChildRsrcRepoIfc;
//...
And I double checked that I am doing the binding in the Repo Svc Provider as follows:
class RepositoryServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(
'MyProj\Repository\Parent\ChildRsrcRepoIfc',
'MyProj\Repository\Parent\EloquentChildRsrcRepo'
);

Shouldn't this be?:
class RepositoryServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(
'MyProj\Repository\Parent\ChildRsrcRepoIfc',
'MyProj\Repository\Parent\EloquentParentRepo '
);
Something that happened to me once while using Laravel IoC automatic resolution was writing
<?
Instead of
<?php
Laravel cannot find the file if you do this. Check your files.

Related

Laravel 5.8 dependecy injection - how to inject model to service

I have BooksService class which should be injected by Book model object. Then I want to inject BooksService to BookController. But I dont know ho to do it.
I am getting an error Class App\Model\BookService does not exist. Is it neccesary to register it somewhere? Also I am not sure if I am doing it right. Is it in this code?
BookService
namespace App\Model;
class BookService
{
/** #var Book */
public $books;
// I am not sure if this is ok
public function construct(Book $books)
{
$this->books = $books;
}
public function test()
{
dd($this->books);
}
}
BookController
namespace App\Http\Controllers;
use App\Http\Requests\StoreBook;
use App\Model\Book;
use App\Model\BookService;
use Illuminate\Http\Request;
class BookController extends Controller
{
....
// This throws me an error BookService does not exists
public function create(BookService $bookService)
{
$bookService->test();
return view('book.create');
}
....
}
So the problem is that __contruct() method expects Book facade instance but Laravel does not know what is it. Also Book facade is available from inside the service class so no need to be injected to it.

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
}

how to access to laravel global Classes in packages

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

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

Grouping event registrations on a service provider

I want to define a service provider that registers events. So have done the following (taken from a book)
<?php
namespace MyApp\Providers;
use Illuminate\Support\ServiceProvider;
class EventsProvider extends ServiceProvider
{
public function boot()
{
Event::listen('some.event', function($parameter)
{
// Handle the event..
});
}
}
Then I added it to the provider array.
But when I execute the code I get the following error
implement the remaining methods
(Illuminate\Support\ServiceProvider::register)
It compels me to declare a register() method.
When I add to the EventsProvider class a method named register() (without implementation, and just make it return null) I get the following error
Class 'MyApp/Providers/EventProvider' not found
Why is that, and how can I solve it?
You just need to declare it, because it's declared in the Interface:
<?php namespace MyApp\Providers;
use Illuminate\Support\ServiceProvider;
class EventsProvider extends ServiceProvider {
public function boot()
{
Event::listen('some.event', function($parameter)
{
// Handle the event..
});
}
public function register()
{
}
}
Then execute
composer dump-autoload --optimize
But you might also have a typo somewhere because it says Class 'MyApp/Providers/EventProviders' not found, but it should be MyApp/Providers/EventProvider.

Resources