I have been trying to use the _forward method to access a method in my IndexController from another controller, but it won't seem to work. I have tried:
$this->_forward('permissions', 'Index')
$this->_forward('permissions', 'index')
$this->_forward('permissions', 'IndexController')
$this->_forward('permissions', 'indexcontroller')
None of which have worked. Here is my code.
Controller
class IndexController extends Zend_Controller_Action
{
/**
* facebookConfig
*
* #var mixed
* #access protected
*/
protected $facebookConfig;
/**
* signed_request
*
* #var mixed
* #access protected
*/
protected $signed_request;
/**
* facebookConfig
*
* #var mixed
* #access protected
*/
protected $permissions;
/**
* init function.
*
* #access public
* #return void
*/
public function init()
{
// get the model
$this->app = new Application_Model_Admin();
// get config data
$this->facebookConfig = $this->app->configData();
// get the apps permissions
$this->permissions = $this->permissions();
// get the data for the head
$data = $this->app->configData();
if(empty($data->ogimage))
$data->ogimage = '';
// set the page title
$this->view->headTitle($data->title, 'PREPEND');
// set the meta data
$this->view->headMeta()->setName('fb:app_id', $data->appid);
$this->view->headMeta()->setName('og:type', $data->ogtype);
$this->view->headMeta()->setName('og:title', $data->ogtitle);
$this->view->headMeta()->setName('og:description', $data->ogdesc);
$this->view->headMeta()->setName('og:url', $data->applink);
$this->view->headMeta()->setName('og:image', $data->ogimage);
}
/**
* permissions function.
*
* #access public
* #return void
*/
public function permissions(){
// return the permissions
return 'publish_stream, read_stream, friends_likes';
}
}
Second Controller
<?php
class ThanksController extends Zend_Controller_Action
{
/**
* facebookConfig
*
* #var mixed
* #access protected
*/
protected $permissions;
/**
* init function.
*
* #access public
* #return void
*/
public function init()
{
// get the model
// get the permissions
$this->permissions = $this->_forward('permissions', 'index');
print_r('<pre>');var_dump($this->_forward('permissions', 'index'));print_r('</pre>');die;
}
}
_forward is used only to call controller actions, not regular methods
_forward('index') actually refers to method indexAction()
Change this:
public function permissions(){
// return the permissions
return 'publish_stream, read_stream, friends_likes';
}
to this:
public function permissionsAction(){
// return the permissions
return 'publish_stream, read_stream, friends_likes';
}
I wasn't thinking particularly clearly, instead I just put the code in a model and accessed the data from their.
Related
Currently, I am working on Facade Pattern which doesn't resolve through the application main Container class where i have implement bind method through the interface.
Index.php
use Bundles\Support\Facades\Route;
Route::get('/users', function () {
return 'Hello';
});
Facade:
Route facade
namespace Bundles\Support\Facades;
class Route extends Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor()
{
return 'route';
}
}
Facade.php
namespace Bundles\Support\Facades;
use Bundles\Foundations\Application;
use RuntimeException;
class Facade
{
/**
* The application instance being facaded.
*/
protected static Application $app;
/**
* The resolved object instances.
*
* #var array
*/
protected static $resolvedInstance;
/**
* Indicates if the resolved instance should be cached.
*
* #var bool
*/
protected static $cached = true;
/**
* #param $method
* #param $args
*/
public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();
if (!$instance) {
throw new RuntimeException('A facade root has not been set.');
}
var_dump($method, $args, static::getFacadeAccessor());
Application::$app->bind(static::getFacadeAccessor(), $args, $method);
exit;
}
/**
* Get the root object behind the facade.
*
* #return mixed
*/
public static function getFacadeRoot()
{
return static::resolveFacadeInstance(static::getFacadeAccessor());
}
/**
* Get the registered name of the component.
*
* #throws \RuntimeException
*
* #return string
*/
protected static function getFacadeAccessor()
{
throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
}
/**
* Resolve the facade root instance from the container.
*
* #param string $name
*
* #return mixed
*/
protected static function resolveFacadeInstance($name)
{
if (isset(static::$resolvedInstance[$name])) {
return static::$resolvedInstance[$name];
}
if (static::$app) {
if (static::$cached) {
return static::$resolvedInstance[$name] = static::$app[$name];
}
return static::$app[$name];
}
}
}
Container.php
namespace Bundles\Containers;
use Bundles\Foundations\Application;
use Bundles\Interfaces\Application as ApplicationContract;
use Bundles\Routes\Route;
class Container implements ApplicationContract
{
/**
* #var Route
*/
public Route $router;
/**
* #var Application
*/
public static Application $app;
// register method called in Application constructor
public function register()
{
$this->router = new Route();
}
/**
* > The `bind` function takes a string as the first argument, and a second argument that can be either a string or a
* closure.
*
* #param string abstract The abstract type to bind to
* #param concrete the class name or a closure that returns an instance of the class
* #param shared whether the object should be shared or not
* #param null|mixed $concrete
* #param mixed $shared
*/
public function bind(string $abstract, $concrete = null, $shared = ''): void
{
var_dump($abstract, $concrete);
exit;
}
}
anyone can help on this?
Thanks in advance
Can i expand the login function in Laravel version 5 and higher without overwriting the standard one. I want to expand the functionality without getting into the vendor folder
enter image description here
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* #var string
*/
protected $redirectTo = '/'; // home initialement
protected $redirectAfterLogout = '/login'; // Added
/**
* Create a new authentication controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => ['logout', 'getLogout']]);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
//
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
//
}
protected function login(array $data)
{
// my changes here...
}
}
I want to pass $this into the sub objects I create on construct However when I dump it I get a **RECURSION** flag.
<?php
namespace Cortana\Framework\System;
use Cortana\Framework\System\ApiAccessor\CacheApiAccessor;
use Cortana\Framework\System\ApiAccessor\ConfigurationApiAccessor;
use Cortana\Framework\System\ApiAccessor\DatabaseApiAccessor;
use Cortana\Framework\System\ApiAccessor\EventsManagerApiAccessor;
use Cortana\Framework\System\ApiAccessor\FilesystemApiAccessor;
use Cortana\Framework\System\ApiAccessor\LocaleApiAccessor;
use Cortana\Framework\System\ApiAccessor\LogApiAccessor;
use Cortana\Framework\System\ApiAccessor\SessionApiAccessor;
use Cortana\Framework\System\ApiAccessor\TranslateApiAccessor;
/**
* System Handler Instance
*
* #author root
*/
class SystemHandler
{
/**
* Development Environment Flag
*
* #var string
*/
const ENV_DEVELOPMENT = 'DEVELOPMENT';
/**
* Staging Environment Flag
*
* #var string
*/
const ENV_STAGING = 'STAGING';
/**
* Test Environment Flag
*
* #var string
*/
const ENV_TEST = 'TEST';
/**
* Production Environment Flag
*
* #var string
*/
const ENV_PRODUCTION = 'PRODUCTION';
/**
* Configuration api accessor instance
*
* #var ConfigurationApiAccessor
*/
protected $configurationApiAccessor;
/**
* Database api accessor instance
*
* #var DatabaseApiAccessor
*/
protected $databaseApiAccessor;
/**
* Translation api accessor instance
*
* #var TranslateApiAccessor
*/
protected $translateApiAccessor;
/**
* Cache api accessor instance
*
* #var CacheApiAccessor
*/
protected $cacheApiAccessor;
/**
* Locale api accessor instance
*
* #var LocaleApiAccessor
*/
protected $localeApiAccessor;
/**
* Events Manager api accessor instance
*
* #var EventsManagerApiAccessor
*/
protected $eventsManagerApiAccessor;
/**
* Log api accessor instance
*
* #var LogApiAccessor
*/
protected $logApiAccessor;
/**
* Session api accessor instance
*
* #var SessionApiAccessor
*/
protected $sessionApiAccessor;
/**
* Filesystem api accessor instance
*
* #var FilesystemApiAccessor
*/
protected $filesystemApiAccessor;
/**
* Array of Loaded Modules
*
* #var array
*/
protected $loadedModules=[];
/**
* System Environment
*
* #var string
*/
protected $environment;
/**
* System Handler Constructor. Loads Accessors
*/
public function __construct()
{
$this->filesystemApiAccessor = new FilesystemApiAccessor();
$this->configurationApiAccessor = new ConfigurationApiAccessor();
$this->databaseApiAccessor = new DatabaseApiAccessor();
$this->cacheApiAccessor = new CacheApiAccessor();
$this->eventsManagerApiAccessor = new EventsManagerApiAccessor();
$this->localeApiAccessor = new LocaleApiAccessor();
$this->logApiAccessor = new LogApiAccessor();
$this->sessionApiAccessor = new SessionApiAccessor();
$this->translateApiAccessor = new TranslateApiAccessor();
}
public function build()
{
}
/**
* Set the system environment
*
* #param string $environment the system environment
* #return SystemHandler
*/
public function setEnvironment($environment)
{
$this->environment = $environment;
return $this;
}
/**
* Return the system environment
*
* #return string the system environment
*/
public function getEnvironment()
{
return $this->environment;
}
/**
* Set the system loaded modules
*
* #param array $loadedmodules array of loaded system modules
* #return SystemHandler
*/
public function setLoadedModules(array $loadedmodules)
{
$this->loadedModules = $loadedmodules;
return $this;
}
/**
* Add a module as a loaded module
*
* #param string $module module to add as a loaded module
* #return SystemHandler
*/
public function addLoadedModule($module)
{
$this->loadedModules[] = $module;
return $this;
}
/**
* Clear loaded modules
*
* #return SystemHandler
*/
public function clearLoadedModule()
{
$this->loadedModules = [];
return $this;
}
/**
* Set the configuration api accessor instance
*
* #param ConfigurationApiAccessor $configurationApiAccessor the configuration api accessor instance
* #return SystemHandler
*/
public function setConfigurationApiAccessor(ConfigurationApiAccessor $configurationApiAccessor)
{
$this->configurationApiAccessor = $configurationApiAccessor;
return $this;
}
/**
* Return the configuration api accessor instance
*
* #return ConfigurationApiAccessor the configuration api accessor instance
*/
public function getConfigurationApiAccessor()
{
return $this->configurationApiAccessor;
}
/**
* Set the database api accessor instance
*
* #param DatabaseApiAccessor $databaseApiAccessor the database api accessor instance
* #return SystemHandler
*/
public function setDatabaseApiAccessor(DatabaseApiAccessor $databaseApiAccessor)
{
$this->databaseApiAccessor = $databaseApiAccessor;
return $this;
}
/**
* Return the database api accessor instance
*
* #return DatabaseApiAccessor the database api accessor instance
*/
public function getDatabaseApiAccessor()
{
return $this->databaseApiAccessor;
}
/**
* Set the translation api accessor instance
*
* #param TranslateApiAccessor $translateApiAccessor the translation api accessor instance
* #return SystemHandler
*/
public function setTranslateApiAccessor(TranslateApiAccessor $translateApiAccessor)
{
$this->translateApiAccessor = $translateApiAccessor;
return $this;
}
/**
* Return the translation api accessor instance
*
* #return TranslateApiAccessor the translation api accessor instance
*/
public function getTranslateApi()
{
return $this->translateApiAccessor;
}
/**
* Set the cache api accessor instance
*
* #param CacheApiAccessor $cacheApiAccessor the cache api accessor instance
* #return SystemHandler
*/
public function setCacheApiAccessor(CacheApiAccessor $cacheApiAccessor)
{
$this->cacheApiAccessor = $cacheApiAccessor;
return $this;
}
/**
* Return the cache api accessor instance
*
* #return CacheApiAccessor the cache api accessor instance
*/
public function getCacheApiAccessor()
{
return $this->cacheApiAccessor;
}
/**
* Set the locale api accessor instance
*
* #param LocaleApiAccessor $localeApiAccessor the locale api accessor instance
* #return SystemHandler
*/
public function setLocaleApiAccessor(LocaleApi $localeApiAccessor)
{
$this->localeApiAccessor = $localeApiAccessor;
return $this;
}
/**
* Return the locale api accessor instance
*
* #return LocaleApiAccessor the locale api accessor instance
*/
public function getLocaleApiAccessor()
{
return $this->localeApi;
}
/**
* Set the events manager api accessor instance
*
* #param EventsManagerApiAccessor $eventsManagerApiAccessor the events manager api accessor instance
* #return SystemHandler
*/
public function setEventsManagerApiAccessor(EventsManagerApiAccessor $eventsManagerApiAccessor)
{
$this->eventsManagerApiAccessor = $eventsManagerApiAccessor;
return $this;
}
/**
* Return the events manager api accessor instance
*
* #return EventsManagerApiAccessor the events manager api accessor instance
*/
public function getEventsManagerApiAccessor()
{
return $this->eventsManagerApiAccessor;
}
/**
* Set the logger api accessor instance
*
* #param LogApiAccessor $logApiAccessor the log api accessor instance
* #return SystemHandler
*/
public function setLogApi(LogApiAccessor $logApiAccessor)
{
$this->logApiAccessor = $logApiAccessor;
return $this;
}
/**
* Return the log api accessor instance
*
* #return LogApiAccessor the log api accessor instance
*/
public function getLogApi()
{
return $this->logApiAccessor;
}
/**
* Set the session api accessor instance
*
* #param SessionApiAccessor $sessionApiAccessor the session api accessor instance
* #return SystemHandler
*/
public function setSessionApiAccessor(SessionApiAccessor $sessionApiAccessor)
{
$this->sessionApiAccessor = $sessionApiAccessor;
return $this;
}
/**
* Return the session api accessor instance
*
* #return SessionApiAccessor the session api accessor instance
*/
public function getSessionApiAccessor()
{
return $this->sessionApiAccessor;
}
/**
* Set the filesystem api accessor instance
*
* #param FilesystemApiAccessor $filesystemApiAccessor the filesystem api accessor instance
* #return SystemHandler
*/
public function setFilesystemApiAccessor(FilesystemApiAccessor $filesystemApiAccessor)
{
$this->filesystemApiAccessor = $filesystemApiAccessor;
return $this;
}
/**
* Return the filesystem api accessor instance
*
* #return FilesystemApiAccessor the filesystem api accessor instance
*/
public function getFilesystemApiAccessor()
{
return $this->filesystemApiAccessor;
}
}
when you create a new class that extends other class, you don't need to pass anything to the constructor, it will be automatically available in the subclass as long as those methods/properties are public or protected. If they are private, you cannot use them in subclass. If you want to use protected/public properties or methods from parent class in your subclass, use them as if you were in the parent class.
class parent{
protected $foo = 'foobar';
protected function get_foo(){
return $this->foo;
}
}
class subclass extends parent{
protected $bar;
public function __construct(){
$this->bar = $this->get_foo();
}
public function get_bar(){
return $this->bar;
}
}
$baz = new bar;
echo $baz->get_bar();
// outputs 'foobar'
all i want to do is add a custom method to my controller so that i can reuse them in other methods inside my controller. eg.:
class SampleController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
generateCode();
sendEmail();
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
/**
* Custom Functions
*/
function sendEmail()
{
}
function generateCode()
{
}
}
Here i just want to be able to call sendEmail() and generateCode() in my index method but i keep getting function not defined.
They are not global functions so you can't use them like that. They are class methods with scope. Note the changes I made for you:
<?php
class SampleController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$this->generateCode();
$this->sendEmail();
}
// . . .
/**
* Custom Functions
*/
private function sendEmail()
{
}
private function generateCode()
{
}
}
You really should do some OOP PHP tutorials, or read some books, or take a course. This is very basic stuff that you should not need to be asking on stack overflow.
http://php.net/manual/en/language.oop5.php
I have a class: ClassA(), i need the Doctrine Service $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');.
I try implements the interface ServiceManagerAwareInterface, but the functions, getServiceLocator() and setServiceLocator(ServiceLocatorInterface $serviceLocator) not work.
Someone used the ServiceLocator outside Controller class in ZF2? It's possible?
<?php
namespace DbSession\Service;
use Zend\Session\SaveHandler\SaveHandlerInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
/**
* Description of SessionDB
*
* #author rab
*/
class SessionDB implements SaveHandlerInterface, ServiceLocatorAwareInterface {
use ServiceLocatorAwareTrait;
/**
* Session Save Path
*
* #var string
*/
protected $sessionSavePath;
/**
* Session Name
*
* #var string
*/
protected $sessionName;
/**
* Lifetime
* #var int
*/
protected $lifetime;
/**
* Constructor
*
*/
public function __construct() {
}
/**
* Open the session
*
* #return bool
*/
public function open($savePath, $name) {
echo "open session";
}
/**
* Close the session
*
* #return bool
*/
public function close() {
echo "close session";
}
/**
* Read the session
*
* #param int session id
* #return string string of the sessoin
*/
public function read($id) {
echo "read session";
}
/**
* Write the session
*
* #param int session id
* #param string data of the session
*/
public function write($id, $data) {
$this->getServiceLocator()->get('');
echo "write";
}
/**
* Destoroy the session
*
* #param int session id
* #return bool
*/
public function destroy($id) {
echo "destroy session";
}
/**
* Garbage Collector
*
* #param int life time (sec.)
* #return bool
*/
public function gc($maxlifetime) {
echo "gc session";
}
}
The easiest way would be to use the ServiceLocatorAwareTrait (part of ZF2). If you do use that make sure your class implements ServiceLocatorAwareInterface (you don't actually have to implement it as the trait does that for you, but you do have to add it to your class' implements clause).
So you get something like this:
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
class ClassA implements ServiceLocatorAwareInterface {
use ServiceLocatorAwareTrait;
}
Then you can just call $this->getServiceLocator() to get a hold of a servicelocator instance.
However if you just need the Doctrine entitymanager it's better practice to just inject this one dependency instead of injecting the ServiceLocator.
Finally solved the problem. My class SessionDatabase() is correct. My instance was wrong.
Implements class:
class SessionDatabase implements SaveHandlerInterface, ServiceLocatorAwareInterface {
use ServiceLocatorAwareTrait;
public function write($id, $data) {
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
}
}
Instance:
private function initDbSession(MvcEvent $e) {
$serviceManager = $e->getApplication()->getServiceManager();
$saveHandler = new SessionDatabase();
$saveHandler->setServiceLocator($serviceManager);
}
Thanks everyone that helped!!! :D