Phalcon : How to access global function? - php

I am totally new to use phalcon.
1. I have created a directory by name library
2. In that directory i have created a file by name commonfunctions.php
3. and i written a
abstract class Commonfunctions extends Phalcon\Mvc\User\Component
{
Public function ProfileImage() //function to set profile pic
{
$auth = $this->session->get('auth');
$user_id = $auth['id']; //get session id
$TblPersonaldetails = TblPersonaldetails::findFirst("user_id = ".$user_id);
echo "pic = ".$profile_pic = $TblPersonaldetails->profile_pic; //get profil_pic path from db
}
}
but I am getting notice message from "Notice: Access to undefined property TblPersonaldetails::profile_pic in D:\xampp\htdocs\test\app\library\Commonfunctions.php on line 98"
How can I resolve this?

This might be what you are looking for.
Service create a folder called Service, Then move your Commonfunctions into the neewly created Service folder. Inside your Commonfunctions file added the namespace *Service*
In your config.php or loader.php do something like
$loader->registerNamespaces(
array(
"Service" => $config->application->servicesDir,
)
);
Your config should have the servicesDir set to something like app/services
Then if that all loads succsefully you can call your method "ProfileImage" like so
$app->getDI()->get('Service\Commonfunctions')
I hope this helps

Related

Using classes with different namespaces in Laravel 8

I am trying to use a library of classes within Laravel 8. I am having difficulty getting the classes to load correctly. I created a new folder within the App folder called DigiSigner, which is the external library's namespace.
App\DigiSigner
DigiSignerClient.php
\libs
BaseRequest.php
Branding.php
ClassLoader.php
Config.php
Curler.php
DigiSignerException.php
DigiSignerResponse.php
Document.php
DocumentField.php
DocumentFields.php
ExistingField.php
ExportObject.php
Field.php
SignatureRequest.php
Signer.php
I created a controller that looks like this
class SignPDFController extends Controller
{
public function getPDF()
{
$client = new DigiSignerClient('client_key');
$request = new SignatureRequest;
$request->setEmbedded(true);
$request->setSendEmails(false);
$template = Document::withID('document_id');
$template->setTitle('Site Title');
$request->addDocument($template);
$signer = new Signer('user#email.com');
$signer->setRole('Signer 1');
$template->addSigner($signer);
$initials = new ExistingField('key');
$initials->setContent('VS');
$signer->addExistingField($initials);
$response = $client->sendSignatureRequest($request);
foreach ($response->getDocuments() as $document) {
foreach ($document->getSigners() as $signer) {
$signDocumentUrl = $signer->getSignDocumentUrl();
}
}
}
}
The DigiSignerClient and the SignatureRequest classes seem to load fine, but the SignatureRequest needs to load the ExportObject class to extend it.
namespace App\DigiSigner;
use App\DigiSigner\libs\ExportObject;
class SignatureRequest extends ExportObject {
I end up with an error like the following.
Error Class 'App\DigiSigner\libs\ExportObject' not found
Namespaces and use are a little fuzzy for me. If someone can point me in the right direction, I would be delighted.
I believe I figured it out. All the files in the subdirectory needed the namespace changed to App\DigiSigner\libs.
Check ExportObject.php namespace.

error not found model after upload into hosting

In localhost, it works fine. After uploading into my hosting i got this error. I'm using zend framework 1.12.
Fatal error: Class 'Application_Models_DBTable_Kereta' not found in /home/mysite/public_html/application/controllers/CarController.php
others post said the problem is because the case sensitivity of file names. But i tried to change and nothing happens. See my attachment for the structured of my project. The attachment shown application and model names.
edited : This problem occurs to all my models class.. can't find models..
Controller code :
class CarController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
//klu login sbgai admin, papar layout admin, klu login sbgai user, papar layout user laaaa,
Zend_Session::start();//start session
$session = new Zend_Session_Namespace("MyNamespace");
$id_pengguna = $session->id_pengguna;
$kategori = $session->kategori;
if($kategori==3)
{
$this->_helper->layout->setLayout('layoutadmin');
}
else
{
}
}
public function indexAction()
{
// $albums = new Application_Model_DbTable_Albums();
//$this->view->albums = $albums->fetchAll();
}
public function reservationAction()
{
if($this->getRequest()->isPost())
{
$jadual_tarikhmula = $this->getRequest()->getPost("jadual_tarikhmula");
$jadual_tarikhtamat = $this->getRequest()->getPost("jadual_tarikhtamat");
$jadual_masamula1 = $this->getRequest()->getPost("jadual_masamula1");
$jadual_masamula2 = $this->getRequest()->getPost("jadual_masamula2");
$jadual_masatamat1 = $this->getRequest()->getPost("jadual_masatamat1");
$jadual_masatamat2 = $this->getRequest()->getPost("jadual_masatamat2");
$simpan = array($jadual_tarikhmula,$jadual_tarikhmula,$jadual_masamula1,$jadual_masatamat1);
$papar = $this->view->dataReserve= $simpan;
$db = new Application_Model_DbTable_Kereta();
$paparkereta = $this->view->reserve = $db->getReservationCar($jadual_tarikhmula,$jadual_tarikhmula,$jadual_masamula1,$jadual_masatamat1);
$this->view->dataWujud = count($paparkereta);
$gambar = $this->view->gambarKereta = $db->getGambarKereta($paparkereta[0]['id_kereta'],false);
}
}
}
Your folder name is DbTable and your model class name is ..._DBTable_... ?
Note that Linux is case-sensitive directory or filename.
And did you add this line to .ini file?
Appnamespace = "Application"
Are you sure you don't have another class named Kereta in /home/mysite/public_html/application/controllers/CarController.php ?
Also, consider cleaning the symfony cache with symfony cc. It's the cache for autoload that is giving you this error I guess.

access class from other class codeigniter

I have this class that I made as a library in codeigniter
class Openstack{
function __construct($params = array())
{
//namespace OpenCloud;
// note that we have to define these defaults BEFORE including the
// connection class
define('RAXSDK_OBJSTORE_NAME','cloudFiles');
define('RAXSDK_OBJSTORE_REGION','ORD');
require_once('lib/rackspace.php');
// these hold our environment variable settings
define('AUTHURL', 'https://identity.api.rackspacecloud.com/v2.0/');
define('USERNAME', "#####");
define('TENANT', "$$$$$");
define('APIKEY', "asdfasdfasdfasdfasdfasdfasdf");
// establish our credentials
$connection = new Rackspace(AUTHURL,
array( 'username' => USERNAME,
'tenantName' => TENANT,
'apiKey' => APIKEY ));
$ostore = $connection->ObjectStore();...
The problem is, when I call a function using:
public function test_upload(){
$this->load->library('opencloud/openstack');
$this->openstack->upload_file("thisworks.pdf", "static/news/10_12.pdf");
}
I get an error saying the class Rackspace isn't found pointing to the line above where I have new Rackspace...
Within the rackspace.php there is a clas name called Rackspace
class Rackspace extends OpenStack{ ...
I have no idea why it can't find it... I know it's loading the file, because I tried to echo something out in there and it showed up....
Any idea what's happening?
EDIT: At the top of the rackspace.php file there is a
namespace OpenCloud;
I don't have that in my library file class Openstack I made... When I put it in there then I got an error "Non-existent class: Openstack"

Why I can't load models in modules?! [Using HMVC in codeigniter 2.1.2]

I hope someone can help me with this one before I jump off the window. I spent few hours on this one and don't know what am I doing wrong.
Basically, I've installed HMVC in CodeIgniter 2.1.2 and everything works fine, BUT for some reason I can't load models the same way I'm doing it in standard controllers. In the old codeigniter 1.7.1 I could use it simply by invoking $this->load->model('my_model') but now I can't?!
Every single time I'm trying to load model I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Special_cart::$db
Filename: core/Model.php
Line Number: 51
I have had installed it step-by-step according to the instructions. I got third_party next to modules folder. In modules I have few modules stored like this:
modules
--boxes
----controller
----models
----views
I invoke module in my code like this:
<?=modules::run('boxes/special_cart/index');?>
My module controller code looks like this:
class Special_cart extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
if ($this->session->userdata('cart'))
{
# get product id's and each quantity
$cart_product_list = array_count_values($this->session->userdata('cart'));
# get list of product_id
$product_list = array_keys($cart_product_list);
# get product details
$this->load->model('productmodel');
$this->load->model('stockmodel');
$cart_products = $this->productmodel->cart_get_products_details($product_list);
$final_cart_array = array();
foreach($cart_products as $cart_product){
$product_stock = $this->stockmodel->view_product_stock($cart_product["id"]);
if(empty($product_stock) || $product_stock["UNITS"]<=0)
$cart_product["UNITS"] = 0;
else{
if($cart_product_list[$cart_product["id_web"]]>$product_stock["UNITS"])
$cart_product["UNITS"] = $product_stock["UNITS"];
else{
$cart_product["UNITS"] = $cart_product_list[$cart_product["id_web"]];
}
}
$final_cart_array[] = $cart_product;
}
$refresh_cart_array = array();
foreach($final_cart_array as $cart_product){
for($i=1;$i<=$cart_product["UNITS"];$i++){
$refresh_cart_array[] = $cart_product["id_web"];
}
}
$this->load->view("special_cart",array(
'refresh_cart_array' => $refresh_cart_array,
'final_cart_array' => $final_cart_array
));
} else {
$this->load->view("special_cart",array(
'refresh_cart_array' => NULL,
'final_cart_array' => NULL
));
}
}
}
I've tried every possible solution found on internet - none of them work....
I hope you understand my problem but in case you need some further explanation please ask me. Can anyone help?
Looks like the model you're trying to load wants to connect to the db, but the database driver is not available. If you use database queries in your application, why don't you load the database driver automatically?
Just insert "database" in the "libraries" array in application/config/autoload.php file. Don't forget to insert your database credentials into application/config/database.php.
$autoload['libraries'] = array('database');
If you need database connection just in one single model, load it before trying to access database library.
$this->load->database();
Try loading the model stating the module name as follows
$this->load->model('module_name/productmodel');
Class Models extends MX_Loader{
function getUser($username){
$sql="SELECT * FROM user WHERE username = ? ";
return $this->db->query($sql,array($username))->row();
}
}
you must using extends MX_Loader because i don't know if using CI_Model the database core cant be load in Codeigniter,,,
Try to use extend MX_Controller class (not CI_Contoller like you are doing atm)
Based on what you have wrote in comment above, I figured that you tried to create new instance of DB in module (based on chrises comment).
Do it on constuctor of Special_cart
So update current construct to be like
public function __construct()
{
parent::__construct();
$this->load->database('default');
}
(I'm writing this from top of my head, so check the methods)
Now for sure db driver should be available in your models.
Regarding issue with HMVC I dont think there are any. I'm using HMVC for a while now, and I found no problems in it (working with databases)
I had the same problem and mistake. I missed to extend controllers to MX_Controller. So the solution would be to change CI_Controller to MX_Controller like this:
class Special_cart extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('productmodel');
$this->load->model('stockmodel');
}
public function index()
{
if ($this->session->userdata('cart'))
{
# get product id's and each quantity
$cart_product_list = array_count_values($this->session->userdata('cart'));
# get list of product_id
$product_list = array_keys($cart_product_list);
# get product details
$cart_products = $this->productmodel->cart_get_products_details($product_list);
$final_cart_array = array();
foreach($cart_products as $cart_product){
$product_stock = $this->stockmodel->view_product_stock($cart_product["id"]);
if(empty($product_stock) || $product_stock["UNITS"]<=0)
$cart_product["UNITS"] = 0;
else{
if($cart_product_list[$cart_product["id_web"]]>$product_stock["UNITS"])
$cart_product["UNITS"] = $product_stock["UNITS"];
else{
$cart_product["UNITS"] = $cart_product_list[$cart_product["id_web"]];
}
}
$final_cart_array[] = $cart_product;
}
$refresh_cart_array = array();
foreach($final_cart_array as $cart_product){
for($i=1;$i<=$cart_product["UNITS"];$i++){
$refresh_cart_array[] = $cart_product["id_web"];
}
}
$this->load->view("special_cart",array(
'refresh_cart_array' => $refresh_cart_array,
'final_cart_array' => $final_cart_array
));
} else {
$this->load->view("special_cart",array(
'refresh_cart_array' => NULL,
'final_cart_array' => NULL
));
}
}
}
this is also explained in the documentation
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/
, here the quote:
Notes:
To use HMVC functionality, such as Modules::run(), controllers must
extend the MX_Controller class. To use Modular Separation only,
without HMVC, controllers will extend the CodeIgniter Controller
class. You must use PHP5 style constructors in your controllers. ie:
<?php
class Xyz extends MX_Controller
{
function __construct()
{
parent::__construct();
}
}

Proper way to use a config file?

I just started using a PHP framework, Kohana (V2.3.4) and I am trying to set up a config file for each of my controllers.
I never used a framework before, so obviously Kohana is new to me. I was wondering how I should set up my controllers to read my config file.
For example, I have an article controller and a config file for that controller. I have 3 ways of loading config settings
// config/article.php
$config = array(
'display_limit' => 25, // limit of articles to list
'comment_display_limit' => 20, // limit of comments to list for each article
// other things
);
Should I
A) Load everything into an array of settings
// set a config array
class article_controller extends controller{
public $config = array();
function __construct(){
$this->config = Kohana::config('article');
}
}
B) Load and set each setting as its own property
// set each config as a property
class article_controller extends controller{
public $display_limit;
public $comment_display_limit;
function __construct(){
$config = Kohana::config('article');
foreach ($config as $key => $value){
$this->$key = $value;
}
}
}
C) Load each setting only when needed
// load config settings only when needed
class article_controller extends controller{
function __construct(){}
// list all articles
function show_all(){
$display_limit = Kohana::config('article.display_limit');
}
// list article, with all comments
function show($id = 0){
$comment_display)limit = Kohana::config('article.comment_display_limit');
}
}
Note: Kohana::config() returns an array of items.
Thanks
If you are reading a group of configuration items for a controller, store them in class member ($this->config), if you are reading a single configuration item; read it individually.
I think first method (A) should be fine, it has lesser code and serves the purpose fine.
If you have site wide stuff that you want access to from "anywhere", another way of doing it may be to put something like:
Kohana::$config->attach(new Kohana_Config_File('global'));
in bootstrap.php. Then create global.php in the application/config directory with something like:
return (array ('MyFirstVar' => 'Is One',
'MySecondVar' => 'Is Two'));
And then when you need it from your code:
Kohana::config ('global.MyFirstVar');
But I suppose all of this comes down to where and how you want to use it.

Categories