how to use different module in zend framework 2 (zf2) - zend-framework2

I have 2 different module in project
1)Album
2)User
I want use some classes of User module in 'Album' Module.
Please help me how I can configure it.
Thanks and Regards,
Ashish

Service Classes should be accessible via the ServiceManager. In case Classes are made available there, either as factories or invokables, you should be able to call them from your Controller like this:
$this->getServiceLocator()->get('name-of-your-service');
Otherwise if it's only classes you need to use, you can simply import and use them like
namespace Othermodule;
use My\Module\Class\Name;
class OtherModuleClass {
public function someAction()
{
new Name();
// Or fully qualified inline
new \Third\Module\Class\Name();
}
}

Related

Can I pass a component to register into an Autofac module?

I have a module, this module has an Interface that the consuming application needs to implement.
I would like to pass that implementation back into my module so that the registration happens in the Module.load() method.
I would expect to pass in a type with a specific base type. (Which I'm not sure is possible either)
I hope this will give me a compile-time error if the module user forgets to register the interface component.
The answer I was looking for eventually worked with generics, the bit of C# that I was not familiar with was the Where clause this allowed you to specify the parent.
And the TAppUSerQuery was a variable I could use in my Autofac config.
my class header looked like this
public class AuthConfigurationModule<TAppUserQuery> :Module
where TAppUserQuery : IAppUserQuery
My Autofac load function looked like this.
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<TAppUserQuery>().As<IAppUserQuery>();
builder.RegisterType<ResolveApplicationUser>().As<IResolveApplicationUser>();
builder.RegisterType<AuthUserRepository>()
.As<IAuthUserRepository>()
.WithParameter(new TypedParameter(typeof(string), _connection));
}

Symfony autowire services failing because two services are based on the same class

I'm starting a new project using Symfony 3.3. I'm would like to use the new autoconfigure/autowiring features but I'm running into an "issue" I'm not sure how to solve.
I have the following services definition coming from an external bundle:
command_bus:
class: Name\Space\To\MessageBusSupportingMiddleware
...
event_bus:
class: Name\Space\To\MessageBusSupportingMiddleware
...
Both services are based on the same "MessageBusSupportingMiddleware" class but their intention is totally different of course.
Now I want Symfony 3.3 to automatically inject the "command_bus" service into my controller. But for this, I would have to use the class in the constructor like this:
public function __construct(
MessageBusSupportingMiddleware $commandBus
){
$this->commandBus = $commandBus;
}
In this case though, Symfony complains because it actually finds several service definition related to this class and so it cannot know which one to provide.
How do you think I could handle this situation ?
I actually find a way to overcome this situation.
I have create two classes in my own project:
class CommandBus extends MessageBusSupportingMiddleware
{
}
class EventBus extends MessageBusSupportingMiddleware
{
}
Their only purpose is to be able to override the default service definition from the external bundle and use different implementation to be able to autowire them. My service override configuration is simply done in a services.yml file as such:
services:
command_bus:
class: CoreBundle\Bus\CommandBus
event_bus:
class: CoreBundle\Bus\EventBus

RedBeanPHP FUSE Model in a Zend Framework 2 application?

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'));

Annotation for WCF DataContractSerializer to replace ArrayOf with a custom name

I have succesfully implemented a RESTful Web Service using the .NET 4.0 framework with MVC 4 and the ApiController class.
I have a method, let's say GetMovies ("/api/movies") that returns an IQueryable<Movie>. Serialization is done using DataContractSerializer, of course. The problem is in the name of the returned list, because it is ArrayOfMovie:
<ArrayOfMovie>
<Movie></Movie>
<Movie></Movie>
...
<Movie></Movie>
</ArrayOfMovie>
I cannot create a custom class, let's say Movies, and add a [CollectionDataContract(Name = "movies")] annotation (as suggested at https://stackoverflow.com/a/4593167/801065) because I cannot extend IQueryable without implementing all of its methods. And I most definitely need an IQueryable for OData/jQuery processing.
How can I solve this? Is there an annotation that can help me?
This is the solution I found.
You need to put a group class in the main class you want to serialize.
[DataContract(Name = "movies")]
public class group
{
[DataMember(Name="movies")]
public IQueryable<Movie> Movies;
}

Extending sfDoctrineRecord in symfony

I've added some functionality to some of my instance classes in my symfony project that I want ALL of my instance classes to have. If I didn't have any qualms about editing the core symfony installation, I would just add my methods directly to the sfDoctrineRecord class. But I don't want to do that, of course, because my changes would break on upgrade, plus my changes wouldn't port well to other projects.
If I want to add certain functionality to all my instance classes in symfony, what's the "right" way to do that?
(P.S. When I say "instance class", I mean something like lib/model/doctrine/Customer.class.php.)
Steps:
Create myDoctrineRecord
abstract class myDoctrineRecord extends sfDoctrineRecord
{
public function commonRecordMethod() { }
}
I place this file in lib/record, but you can put it anywhere that the autoloader will see it.
Set Symfony to use this class in the configureDoctrine callback of your ProjectConfiguration:
public function configureDoctrine(Doctrine_Manager $manager)
{
sfConfig::set('doctrine_model_builder_options', array('baseClassName' => 'myDoctrineRecord'));
}
That's it! Isn't Symfony great? :)
I suppose the proper way would probably be to add a Doctrine_Template to the models in question, however you would need to define it as a behavior for every model in your schema.yml
class MyMethodsTemplate extends Doctrine_Template
{
public function customMethod1(){
$model = $this->getInvoker();
//do stuff with model
}
public function customMethod2(){
$model = $this->getInvoker();
//do stuff with model
}
}
And then in your schema.yml:
ModelName:
actAs:
MyMethodTemplate: ~
# the rest of your definition
After you rebuild you should be able to call:
$model = new ModelName();
$model->customMethod1();
$model->customMethod2();
Of course Doctrine templates and listeners are much more powerful than that. You should take a look at the documentation for decent overview

Resources