I'm new to Lumen, and have a fresh install (v8.2.4) and have followed the docs, trying to write my own service, but I keep getting error
"Target class [App\Prodivers\BatmanServiceProvider] does not exist."
Like I said, its a fresh install according to the Lumen docs.
in /bootstrap/app.php
$app->register(App\Providers\BatmanServiceProvider::class);
in /app/Providers/BatmanServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class BatmanServiceProvider extends ServiceProvider
{
public function register()
{
return "batman!";
}
}
My controller: app/Http/Controllers/MainController.php
<?php
namespace App\Http\Controllers;
use App\Prodivers\BatmanServiceProvider;
class MainController extends Controller{
public function __construct(BatmanServiceProvider $BatmanServiceProvider){
}
public function main(){
print "hello space!";
}
}
What am I missing/doing wrong?
in /bootstrap/app.php
$app->register(App\Providers\BatmanServiceProvider::class);
in /app/Providers/BatmanServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\BatmanService;
class BatmanServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(BatmanService::class, function(){
return new BatmanService;
});
}
}
create Services folder in your_lumen_project/app, and create php file BatmanService.php
in /app/Services/BatmanService.php
<?php
namespace App\Services;
class BatmanService
{
public function sayHello(){
return 'hi, space!';
}
}
Now, you can use anywhere!
<?php
namespace App\Http\Controllers;
use App\Services\BatmanService;
class MainController extends Controller{
protected $batmanService;
public function __construct(BatmanService $batmanService){
$this->batmanService = $batmanService;
}
public function main(){
return $this->batmanService->sayHello(); // "hi, space!"
}
}
Related
I have this code
Controller
<?php
namespace App\Exchange\Helpers;
use App\Contracts\Exchange\Notification;
class Locker
{
protected $notification;
public function __construct(Notification $notification)
{
$this->notification = $notification;
}
public function index()
{
return $this->notification->sendMessage('test');
}
Interface
<?php
namespace App\Contracts\Exchange;
interface Notification
{
public function sendMessage($message);
}
File Kernel.php
namespace App\Providers;
use App\Contracts\Exchange\Notification;
use App\Exchange\Helpers\Notification\Telegram;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(Notification::class, function (){
return new Telegram(env('TELEGRAM_EXCHANGE_TOKEN'), env('TELEGRAM_EXCHANGE_CHAT_ID'));
});
}
If I try to use new Locker(); I get a TypeError error: Too few arguments to function App\Exchange\Helpers\Locker::__construct(), 0 passed in Psy Shell code on line 1 and exactly 1 expected
Your controller should extend Illuminate\Routing\Controller in order for dependency injection to work. Or just refactor your __construct method using app helper:
<?php
namespace App\Exchange\Helpers;
use App\Contracts\Exchange\Notification;
class Locker
{
protected $notification;
public function __construct()
{
$this->notification = app(Notification::class);
}
public function index()
{
return $this->notification->sendMessage('test');
}
}
I am trying to use Laravel view composer. I have registered my class in config/app.php but I keep getting the following error:
"Class App\Http\ViewComposers\PostComposer does not exist
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use View;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
View::composer('plain','App\Http\ViewComposers\PostComposer');
}
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
}
my post composer class
<?php
namespace App\Http\ViewComposer;
use Illuminate\View\View;
use App\Post;
class PostComposer
{
public function comspose(View $view)
{
$posts = Post::all();
$view->with('postha', $posts );
}
}
and here is the screenshot of my browser:
![folder structure in my app][]
Your namespace is wrong.
You're importing from (plural):
App\Http\ViewComposers\PostComposer
but the namespace of your ViewComposer isn't plural:
App\Http\ViewComposer
try it : namespace App\Http\ViewComposer To namespace App\Http\ViewComposers
I'm new in laravel and i need some help with laravel 5, why is this erro? the error is in Composer class and ComposerServiceProvider
MainComposer class
<?php
namespace App\Htt\Composers;
use Illuminate\Contracts\View\View;
use App\Category;
class MainComposer {
public function compose(View $view){
$view->with('categories', Category::all());
}
}
ComposerServiceProvider.php
<?php
namespace App\Providers;
use View; // its saing class View does not exist
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider{
public function boot(){
View::composer('*', 'App\Http\Composers\MainComposer');
}
public function register(){
}
}
I have a Laravel app that requires getting some config vars that need to be used by most of my controllers.
Therefore it seems like this would be the perfect time to use middleware.
Is this the correct use of middleware? and if so, once the middleware gets the config vars, is it best practice to add these to the request object so they can be accessed by my controller?
Thanks to any responders.
J
Not, definitely!
Actually (based on you've written), the best way to go is creating an application service and registering this service on Service Container - App\Providers\AppServiceProvider (in app/Providers/AppServiceProvider.php).
Something like this:
<?php
# The Config Service:
namespace App\Services;
/**
* Config Manager
*/
class Config
{
/** #var SomeDependency */
protected $dependency;
public function __construct(SomeDependency $dependency)
{
$this->dependency = $dependency;
}
public function getVar($var)
{
// ...
}
}
In your Service Provider:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
//...
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->registerConfigManager();
}
public function registerConfigManager()
{
$this->app->singleton('config_service', function ($app) {
return new \App\Services\Config(new \SomeNamespace\SomeDependency);
});
}
//...
}
And now you can to access the service container via app(), like this:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
public function index(Request $request)
{
app('config_service')->getVar('key');
//...
}
}
IMO, middlewares are made for pre-processing requests, restrict user access, and other security related.
I would simply load the configuration in the main Controller class and use it in the extending controllers.
For example:
base controller
namespace App\Http\Controllers;
uses goes here ...;
class Controller extends BaseController
{
protected $configs = [];
public function __construct() {
$this->loadConfigs();
}
protected function loadConfigs()
{
//read configuration files or tables in database
//and put the values into '$this->configs';
}
}
user controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class User extends Controller
{
public function index()
{
echo $this->configs['toolbar.color']; //just an example
}
}
I've bind my interface called CustomerRepository to EloquentCustomerRepository. This is my CustomerServiceProvider:
public function register()
{
$this->app->bind(CustomerRepository::class,EloquentCustomerRepository::class);
$this->app->bind(PackageRepository::class,EloquentPackageRepository::class);
}
When I try to instantiate it in my controller like this:
<?php
namespace App\Http\Controllers\api\v1;
use Lsupport\repositories\api\v1\customer\CustomerRepository;
use App\Http\Controllers\Controller;
use Lsupport\customer\Customer;
use App\Http\Requests;
class CustomerController extends Controller
{
protected $CustomerRepository;
public function __construct(CustomerRepository $CustomerRepository)
{
$this->CustomerRepository = $CustomerRepository;
}
It throws the following error:
Target [Lsupport\repositories\api\v1\Customer\CustomerRepository] is not instantiable while building [App\Http\Controllers\api\v1\CustomerController].
I also registered it in app.config:
App\Providers\CustomerServiceProvider::class,
What am I doing wrong?
CustomerServiceProvider
<?php
namespace App\Providers;
use Lsupport\repositories\api\v1\customer\EloquentCustomerRepository;
use Lsupport\repositories\api\v1\customer\EloquentPackageRepository;
use Lsupport\repositories\api\v1\customer\CustomerRepository;
use Lsupport\repositories\api\v1\customer\PackageRepository;
use Illuminate\Support\ServiceProvider;
class CustomerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind(CustomerRepository::class,EloquentCustomerRepository::class);
$this->app->bind(PackageRepository::class,EloquentPackageRepository::class);
}
}
CustomerRepository
<?php
namespace Lsupport\repositories\api\v1\Customer;
interface CustomerRepository
{
public function create($request);
}
**EloquentCustomerRepository**
<?php
namespace Lsupport\repositories\api\v1\customer;
use Lsupport\repositories\api\v1\customer\CusteromRepositoryTrait;
use Lsupport\repositories\api\v1\remain\RightTrait;
use Lsupport\repositories\api\v1\remain\JsonTrait;
use Lsupport\customer\Customer;
class EloquentCustomerRepository implements CustomerRepository
{
use JsonTrait;
use RightTrait;
use CustomerRepositoryTrait;
code.....
Ok, the first thing I notice is that you probably want the same namespaces on the interface and on the class. So, the namespace of EloquentCustomerRepository should be
namespace Lsupport\repositories\api\v1\Customer;
and not
namespace Lsupport\repositories\api\v1\customer;
(with lower customer).
Now, on your CustomerServiceProvider, you should use:
public function register()
{
$this->app->bind('Lsupport\repositories\api\v1\Customer\CustomerRepository', 'Lsupport\repositories\api\v1\Customer\EloquentCustomerRepository');
}
Make sure you run composer dumpautoload -o on the command line.