ZfcUser extend User class with additional get function - zend-framework2

I'm new to the Zend2 Framework and have installed ZfcUser with an added database column which I would like to access through:
<?php echo $this->zfcUserIdentity()->getOrg(); ?>
Any help on extending the User Class to access this variable would be greatly appreciated.
Ryan

Extend the ZfcUser user entity to include your new property and accessors. You'll need to do this in your own module, or if you're using the skeleton app, in the Application module will work.
<?php
namespace Application\Entity;
use ZfcUser\Entity\User;
class MyUser extends User
{
protected $org;
public function setOrg($org)
{
$this->org = $org;
return $this;
}
public function getOrg()
{
return $this->org;
}
}
Copy vendor/ZfcUser/config/zfcuser.global.php.dist to /config/autoload/zfcuser.global.php
Open the file you just copied in your editor, and find the section below
/**
* User Model Entity Class
*
* Name of Entity class to use. Useful for using your own entity class
* instead of the default one provided. Default is ZfcUser\Entity\User.
* The entity class should implement ZfcUser\Entity\UserInterface
*/
//'user_entity_class' => 'ZfcUser\Entity\User',
uncomment the line, and replace the value with the fully qualified class name of the MyUser entity you created
'user_entity_class' => 'Application\Entity\MyUser',
Then try accessing your method
<?php echo $this->zfcUserIdentity()->getOrg(); ?>

Related

How to get my own ICultureSelector implementation injected in the framework

I have implemented my version of ICultureSelector, in a custom module.
Here it is a part of its definition (my question is not about the logic to select the culture; I tried with my own namespace and also with a namespace same as the namespace used in the module Orchard.Localization):
namespace Orchard.Localization.Selectors
{
[OrchardFeature("Orchard.Localization.CultureSelector")]
public class ShortRouteCultureSelector : ICultureSelector
{
public CultureSelectorResult GetCulture(HttpContextBase context)
{
...
I put a breakpoint in the method GetCurrentCulture of the class CurrentCultureWorkContext in the Orchard.Framework project, and I see that the variable IEnumerable _cultureSelectors contains all the implementation of ICultureSelector of the module Orchard.Localization but not my implementation, that is never used.
What am I missing?
Remove the OrchardFeature attribute or define your own feature name as Orchard.Localization.CultureSelector is defined in Orchard.Localization module already.

laravel route and controller not working

Laravel 5.1
//////////////////////////////////////////////////////
My Route://
Route::resource('/books', 'BookController#index');
////////////////////////////////////////////////////////
My BookController//
<?php
namespace App\Http\Controller;
use App\Book;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BookController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$books=Book::all();
return view('books.index',compact('books'));
}
}
/////////////////////////////////////////////////////////
My Url:
http://localhost:8000/books
/////////////////////////////////////////////////////////
My Browser show this error//
Whoops, looks like something went wrong.
1/1 ReflectionException in
C:\xampp\htdocs\bookstore\vendor\laravel\framework\src\Illuminate\Container\Container.php
line 737:
Class App\Http\Controllers\BookController does not exist
Notice the error says Class App\Http\Controllers\BookController does not exist. That doesn't match your namespace set in the BookController class.
It's looking for your class in the "App\Http\Controllers" location with an "s". Your namespace says "App\Http\Controller" (without the "s"). Fix your namespace and it should work.
You are using a resourceful route and you don't need to specify the method.Also the / before the books is not needed.
Your route should be like this:
Route::resource('books', 'BookController');

How do I include or autoload external libraries in a TYPO3 Extbase Extension? + Dependecy Injection?

I'm developing a TYPO3 4.6 Extension with Extbase 1.4 and im trying to include an external library. The library, in my case the facebook PHP SDK, is under $_EXTKEY/Resources/PHP/facebook-php-sdk/facebook.php. I would like the library to autoload and automatically inject (Dependecy Injection) where I need it.
Some comments I found online suggest that one should include libraries with require_once():
http://forge.typo3.org/issues/33142
if it's just a tiny helper library, it's intended to be stored in {PackageRoot}/Resources/PHP/{libraryName} and just included via require. is this suspected by the problem however?
if the FLOW3 package mainly represents the foreing library at all, like it's the case in Imagine or Swift package, the library code is put below {PackageRoot}/Classes directly."
http://lists.typo3.org/pipermail/typo3-project-typo3v4mvc/2011-July/009946.html
"I would include the class (using require_once) from within a specific action to handle this. That way you have access over those functions and the class becomes your library."
I tried this and it works like this:
<?php
require_once( t3lib_extMgm::extPath('extkey') . 'Resources/PHP/facebook-php-sdk/facebook.php');
class Tx_WsLogin_Domain_Repository_FacebookUserRepository extends Tx_WsLogin_Domain_Repository_UserRepository {
protected $facebook;
public function __construct() {
$this->setFacebook(new Facebook(array(
'appId' =>'',
'secret' => '')
));
parent::__construct();
}
public function setFacebook(Facebook $facebook) {
$this->facebook = $facebook;
}
public function sampleFunction() {
$userId = $this->facebook->getUser();
}
}
?>
But how can I get it to autoload and automatically inject the library with the injectFacebook function?
edit:
Like #alex_schnitzler and #sorenmalling mentioned about autoloading:
#PeterTheOne Put all the files inside ext_autoload.php and then use DI or the object manager.
#PeterTheOne put the class definition into ext_autoload.php in your extension?
I tried it like this (file: ext_autoload.php):
<?php
$extPath = t3lib_extMgm::extPath('extKey');
return array(
'facebook' => $extPath . 'Resources/PHP/facebook-php-sdk/facebook.php',
);
?>
It seems to find and include the right file. But when I try to user Dependency Injection (like peter answered) I get an error:
not a correct info array of constructor dependencies was passed!
InvalidArgumentException thrown in file /var/syscp/webs/web1/dev/typo3_src-4.5.15/typo3/sysext/extbase/Classes/Object/Container/Container.php in line 247.
I think this is because the constructor of the Facebook class has a required $config argument.
edit2:
I did what peter said in his answer and with the help of #alex_schnitzler and #sorenmalling, who pointed me to the ObjectManager, my FacebookService looks like this now:
class Tx_Extkey_Service_FacebookService implements t3lib_Singleton {
/**
* #var Tx_Extbase_Object_ObjectManagerInterface
*/
protected $objectManager;
/**
* Facebook from #link https://github.com/facebook/facebook-php-sdk facebook-php-sdk
*
* #var Facebook
*/
protected $facebook;
/**
* #param Tx_Extbase_Object_ObjectManagerInterface $objectManager
*/
public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
$this->objectManager = $objectManager;
}
/**
*
*/
public function initializeObject() {
$this->facebook = $this->objectManager->create(
'Facebook',
array(
'appId' =>'input appId here',
'secret' => 'input app secret here'
)
);
}
/**
* #return Facebook
*/
public function getFacebook() {
return $this->facebook;
}
}
For more help read: http://forge.typo3.org/projects/typo3v4-mvc/wiki/Dependency_Injection_(DI) the parts about initializeObject() and Creating Prototype Objects through the Object Manager
First create ext_autoload.php in extension root folder
and add your code,it contain single dimension array with key as class name(class name must be prefix with extension key) and value as path to file.
make sure clear your site
<?php
$extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('rent_system');
return array(
'rent_system_TCPDF' => $extensionPath.'Resources/Private/PHP/tcpdf/tcpdf.php',
);
?>
In controller file
$pdf = $this->objectManager->create('rent_system_TCPDF');
Extbase injection is pretty simple. Here's the actual implementation. Using external libraries, however, is not.
Once you figure out how to load the library, have you tried just injecting it? Like so:
/**
* #var Facebook
*/
protected $facebook;
/**
* inject the facebook
*
* #param Facebook facebook
* #return void
*/
public function injectFacebook(Facebook $facebook) {
$this->facebook = $facebook;
}
NOTE: You need the #param in the comment and you also need to clear your configuration cache after adding this code.
I don't know about the Facebook SDK API, but hopefully you can instantiate the Facebook object with the default constructor and then add the arguments later with setter methods. You might want to create a FacebookService class (singleton) that loads the Facebook PHP and sets the essential arguments. Then you can inject a FacebookService to get the actual Facebook object whenever you need it.

How to use a Component with Admin Generator?

I know we can use components with the admin generator (thanks to ~ symbol).
However, in the components.class.php, how to call the auto-generated class ?
At this moment, I'm using this :
require_once dirname(__FILE__).'/../lib/commissionGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/commissionGeneratorHelper.class.php';
class commissionComponents extends autoCommissionComponents
{
}
But I obtain this error :
Fatal error: Class 'autoCommissionComponents' not found in /home/site/liguelorraine/apps/saSecureLigueLorraine/modules/commission/actions/components.class.php on line 7
There are no automatically generated component classes. Just extends sfComponents as usual.

sfDoctrineGuard - how to ALWAYS join sfGuardProfile to sfGuardUser

I want to make it so that anytime the db is queried for an sfGuardUserProfile it is autmoatically joined and hydrated with its related sfGuardUser.
If i was using Propel 1.2 i would normally override the doSelectStmt method of the sfGuardUserProfilePeer class to inspect the Criteria and modify it as necessary as well as modifying the hydrate method of the sfGuardUserProfile class. Im not sure how to go about doing this in Doctrine though.
You could use Event Listeners. Read more about them in the doctrine documentation: Event Listeners
In symfony 1.4 sfGuardUser can be modified. It's by default in lib/model/doctrine/sfDoctrineGuardPLugin/sfGuardUser.class.php. You can add following preDqlSelect() method to modify the query. Note that it's not tested.
class sfGuardUser extends PluginsfGuardUser
{
public function preDqlSelect($event)
{
$params = $event->getParams();
$query = $event->getQuery();
$alias = $params['alias'] . '.Profile';
if ((!$query->isSubquery() || ($query->isSubquery() && $query->contains(' ' . $params['alias'] . ' '))) && !$query->contains($alias))
{
$query->innerJoin($alias);
}
}
}
To make it working you need to have DQL callbacks turned on. You can do it in your ProjectConfiguration class:
class ProjectConfiguration extends sfProjectConfiguration
{
public function configureDoctrine(Doctrine_Manager $manager)
{
$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
}
}
Although I agree with Coronatus, I think what you're looking to do can be achieved with:
http://www.symfony-project.org/plugins/sfGuardPlugin
See "Customize the sfGuardUser model".
Basically, the profile needs to be called "sf_guard_user_profile" and the relation set up, and then you should be able to use:
$this->getUser()->getGuardUser()->getProfile();
I think the right profile model name is needed for some config file purposes but I may be wrong.

Resources