So I've been dependency-injecting all facade-referencing classes into my controller today as seen on Taylor Otwell's latest blog post here:
http://taylorotwell.com/response-dont-use-facades/
The following constructor injects its dependencies properly:
public function __construct(
Illuminate\Session\Store $session,
Illuminate\Routing\Redirector $redirect,
Illuminate\View\Environment $view
) {
...
}
I am using the facade class reference for this:
http://laravel.com/docs/facades#facade-class-reference
However, when I try and inject the mailer class, I get the error "class mailer does not exist". I am trying like so:
public function __construct(
Illuminate\Session\Store $session,
Illuminate\Routing\Redirector $redirect,
Illuminate\View\Environment $view,
Illuminate\Mail\Mailer $mailer
)
The class obviously does exist, and the facade class reference appears to be correct too. I don't know what could be causing this error.
I'm additionally having the same problem with the Validator class, but I assume the solution would be the same.
Turns out there was a small bug in the framework source.
Related
I wish to make DAO layer in my grails project which would be not be associated with any of the domain classes and would be interacting with the secondary database of my project. I get the following error when I try to inject the service in any controller:
"Cannot invoke method abc() on null object"
However, the error is resolved and works perfectly when I initialise the service using the new keyword in the controller but I know that shouldn't be necessary as grails is supposed to handle it. Can anyone tell me what am I missing?
I don't think the issue has anything to do with whether or not the service is associated with a domain class. The DI container doesn't know anything about that.
If you have a controller like this:
// grails-app/controllers/demo/SomeController.groovy
package demo
class SomeController {
SomeService someService
def someControllerAction() {
someService.abc()
// ...
}
}
And a service like this...
// grails-app/services/demo/SomeService.groovy
package demo
class SomeService {
void abc() {
// ...
}
}
That will work fine.
It is almost impossible to say for sure without seeing what your code what you are doing wrong but one possibility is something like this, which will not work:
// grails-app/controllers/demo/SomeController.groovy
package demo
class SomeController {
def someControllerAction() {
// This is a local variable, not
// a property and as such will not
// be subjected to dependency injection.
SomeService someService
// ...
someService.abc()
// ...
}
}
Also, make sure the property name (someService in the sample above) matches the service class name, but with a lower case first letter (more generally, make sure the property name matches the property name representation of the service class name, which is usually as simple as lower casing the first letter of the class name).
Suppose I have 3 modeules in OpenGraph. i.e for example
ObjectGraph og = new ObjectGraph.create(
new moduleA();
new moduleB();
new moduleC();
);
#module
public class moduleA{
#Provides #Singleton Log providerLog(){
return log;
}
}
till now it seems to be okay. But I have a class named
publi class BaseActivity extends Activity {
#Inject
Log log;
}
Here i am not specifiying any module in the #Injection. But Log object will be created in the class. I know this log object will be created from that moduleA module like injects{#moduleA}. How it will create Log module without specifying any module.
Anything wrong with my understanding or coding please suggest me. Thx in advance.
You have specified moduleA in the object graph. You will be injecting your BaseActivity class using the object graph, which is configured using moduleA.
I'm trying to use the built-in laravel's Ioc container to inject a PageManager class inside a Page model and I'm a little lost.
What I'm trying to achieve is something like that:
class Pages extends Eloquent {
public function __construct(PagesManagerInterface $manager, array $attributes = array())
{
parent::__construct($attributes);
$this->manager = new $manager;
}
public function saveToDisk()
{
$this->manager->writeToFile();
}
But I obtain this error:
ErrorException: Argument 1 passed to Pages::__construct() must be an instance of PagesManagerInterface, none given.
I tried to add this in app/start/global.php:
App::bind('Pages',function(){
return new Pages(new PagesManager);
});
But is seems ignored by the framework, and also i don't know how to insert the $attribute array into this declaration.
I'm a little lost so any help is appreciated!
It's not a good idea to overload a model's constructor because new instances can be spawned behind the scenes through various methods, like Model::find().
When that happens, the dependencies you're asking for in your custom constructor aren't being passed in because the Model class isn't aware of them. So, you get that error message.
See the find() method here: http://laravel.com/api/source-class-Illuminate.Database.Eloquent.Model.html#380-397
See this post by Jason Lewis: http://forums.laravel.io/viewtopic.php?pid=47124#p47124
I think that what you need is:
App::bind('PagesManagerInterface',function(){
return new Pages(new PagesManager);
});
This tells Laravel to inject a new Page object everytime it needs an instance of your PagesManagerInterface wich wasn't passed while creating the model.
In Laravel you can use the IoC Container:
public function saveToDisk(){
$managerObject = app()->make('path\to\class\PagesManagerInterface');
$managerObject->writeToFile();
}
I'm trying to use the ORM RedBeanPHP (v3.3) in a ZF2 (v2.0.2) application and I'm having trouble with its automatic FUSE model. I can't make it link to my model classes. It's not picking them up automatically and using "regular" beans instead.
I'm using the RjhRedbean module to load up RedBean in ZF2.
My model class is the following, placed in the folder .\module\Check\src\Check\Model\Model.
<?php
namespace Check\Model;
use \RedBean_SimpleModel;
class Model_Check extends RedBean_SimpleModel
{
public $id;
public $type;
...
public function open()
{
}
public function toArray()
{
return array($this->id, $this->type);
}
I confirm it's picked up by the autoloader since $c = new Model_Check(); works.
My controller code trying to load all the Check model objects from the DB is:
<?php
namespace Check\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use RjhRedbean;
use Check\Model\Model_Check;
class CheckController extends AbstractActionController
{
public function listAction()
{
$rb = $this->getServiceLocator()->get('RjhRedbean');
$checks = $rb->findAll('Check'); // does not link to my Model object
foreach ($checks as $check) {
$ar = $check->toArray(); // does not exist in the objects returned
...
The objects returned are RedBean_OODBBean
What should I put as the bean name in the findAll() method? I tried:
$checks = $rb->findAll('Model_Check');
$checks = $rb->findAll('Check/Model/Check');
$checks = $rb->findAll('Check/Model/Model_Check');
Nothing seems to do the trick. When creating a bean, I get the same problems too...
Thanks.
The reason that this isn't working is that FUSE cannot automatically find the Model for the bean as it would normally do due to problems introduced in using namespaces.
You can work around this problem by using a class map file and defining all Models that you are using in there. Making sure you models are in the global namespace.
More information and examples can be found on my blog: How to use FUSE models in RjhRedbean
I modified RjhRedbean module as followed:
1.Created a class named ModelFormatter
this class does nothing else but return the appropriate namespace related to your models.
the namespace can be set in your config.
2.In the RjhRedbeanServiceFactory class added
RedBean_ModelHelper::setModelFormatter($serviceLocator->get('ModelFormatter'));
i have a code like this, but giving error can not access protected member.
**Class A
{
protected void m1()
{
some code
}
}
Class B:A
{
B b=new B();
b.m1();// Ok works fine
A a =new a();
a.m1();/// don't work, compile time error
A a2=new B();
a2.m1(); //compile time error, don't work
}**
just not getting reason behind this, Why so aberrant nature of the above code, why a method of a class using same class object not accessible out side. While i searched a bit for this but did not undersand, i found something that compiler nature come in picture, but i did not understand.
you can prefix your call by base keyword for calling base members. protected means that a member could be inherited by descendants and could be called but through the specified way.
calling a.m1() in your class hasn't any difference with calling it form an out of B code.
As stated above, the protected method inside the class can only be accessed by the class that inherits the original container.In your case B is the sub class, a is your base. Inside your B class you are creating a new instance of A class ('a'), and this is now a different object (B class does not care about this new object A). This new object 'a' will not expose the protected method, so 'a.m1()' will throw a compile error.