Dynamically create a new object from a class with different namespace - php

I'm trying to create a small RESTful API for my database and I encountered a problem creating controller object dynamically based on user request, because all my code is using namespaces and just doing:
$api = new $controllerName($request);
Won't work. Because $controllerName would resolve to "ReadController", but is actually \controllers\lottery\ReadController hence the error
The whole part of defining the path to the class is:
if ($method === 'GET') {
$controllerName = 'ReadController';
// #NOTE: $category is a part of $_GET parameters, e.g: /api/lottery <- lottery is a $category
$controllerFile = CONTROLLERS.$category.'/'.$controllerName.'.php';
if (file_exists($controllerFile)) {
include_once($controllerFile);
$api = new $controllerName($request);
} else {
throw new \Exception('Undefined controller');
}
}
And the declaration of ReadController in core\controllers\lottery\ReadController.php
namespace controllers\lottery;
class ReadController extends \core\API {
}
Any ideas how to dynamically create the object?
Thanks!

$controllerName = 'controllers\lottery\ReadController';
new $controllerName($request);
Classes instantiated from strings must always use the fully qualified class name.

Related

Cannot instantiate Object dynamically in PHP - Laravel [duplicate]

The autoloader works when I use it in index.php, but when I create an object within index.php and this object has to create other objects (which are all in the same namespace), it throws the error Uncaught Error: Class 'xxx' not found in (...).
My composer.json looks like this:
{
"autoload": {
"psr-4": {
"pizzaCase\\": "src",
"Connection\\": "src/Connection/",
"Elements\\": "src/Elements/"
}
},
"require": {
"cboden/ratchet": "^0.4"
}
}
My index.php looks like this:
<?php
require_once __DIR__. '/vendor/autoload.php';
require_once __DIR__."/src/config.php";
use Connection\Database;
use Elements\Form;
use Elements\FormElement;
use Elements\FormElementRadio;
// Database::init();
$form = new Form();
$data["options"] = "soemthing, something else";
$form->addElement("", "pizza", "", "Choose pizza", "radio", $data);
?>
In the addElement method I then create an object which is also within the src/Elements/ namespace, but it throws the error mentioned above.
The body of my addElement method looks like this:
<?php
namespace Elements;
class Form
{
public static $leftSize = 3;
protected $elements = [];
public function addElement($table, $name, $value, $label=false, $type = false, $data = false)
{
$type = ucfirst($type);
$class = "FormElement{$type}";
//FAILS HERE
if(class_exists($class))
{
//CLASS EXISTS, CREATE OBJECT FROM RESPECTIVE CLASS
$form = new $class($table, $name, $value, $label, $type, $data);
$this->elements[$name] = $form;
}
}
}
What am I doing wrong (or missing)? How come the autoloader can autoload it from index.php, but the object I create cannot create other objects without autoloader failing?
The difference is not to do with where the code is being run; the difference is that the failing code is trying to choose which class to load dynamically.
In PHP, namespaces are essentially a compile-time feature: before any of your code is run, the compiler looks at all references to class names which don't start with \, and prefixes them with the current namespace, or according to rules you've specified with use statements. When the code runs, the current namespace, and use statements, aren't visible at all.
When you specify a class name dynamically, the compiler just sees a string, not a class name, so leaves it alone. Then when the code runs, the class name looked up is assumed to be fully specified, not relative to the current namespace or use statements.
So the solution is simple - specify the full namespace when creating the dynamic class name:
$class = "Elements\FormElement{$type}";
You can also use the magic constant __NAMESPACE__ to have the compiler substitute the current namespace name for you (obviously, this still won't account for any use statements):
$class = __NAMESPACE__ . "\FormElement{$type}";
Alternatively, if you have a specific set of classes you are choosing between, you can use the ::class syntax to generate a string at compile time, based on the current namespace and any use statements in effect:
$mapTypeToClassName = [
'Radio' => FormElementRadio::class, // expands to 'Elements\FormElementRadio'
'Select' => FormElementSelect::class,
// etc
];
$class = $mapTypeToClassName[$type];
It could be because you’re having multiple namespaces for the src directory.
Usually you would just create a namespace for src like this
“psr-4": {
"PizzaCase\\": "src"
}
And then just use PizzaCase\Elements and PizzaCase\Connections as namespaces

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.

PHP Slim Get route placeholder in a container

Is it possible get the value of a route placeholder within a Slim container? I know I can access the placeholder by adding a third parameter to the request but I'd like to have it injected so I'm not assigning it on each request.
I've tried $app->getContainer('router') but I can't seem to find a method to actually pull the placeholder value.
Example:
$app = new Slim\App;
$c = $app->getContainer();
$c['Controller'] = function() {
$userId = // how do I get the route placeholder userId?
return new Controller($userId);
};
$app->get('/user/{userId}','Controller:getUserId');
class Controller {
public function __construct($userId) {
$this->userId = $userId;
}
public function getUserId($request,$response) {
return $response->withJson($this->userId);
}
}
Without some 'hacky' things this will not work because we have no access on the request object build by slim, while the controller get constructed. So I recommend you to just use the 3rd parameter and get your userid from there.
The 'hacky' thing would be todo the same, what slim does when you execute $app->run(), but if you really want todo this, here you'll go:
$c['Controller'] = function($c) {
$routeInfo = $c['router']->dispatch($c['request']);
$args = $routeInfo[2];
$userId = $args['userId'];
return new Controller($userId);
};
Note: slim3 also urldecoded this values so may do this as well urldecode($args['userId']) Source
create a container wrapper and a maincontroller then extend all your controller from your maincontroller, then you have access to the container.
here is how i solved this problem:
https://gist.github.com/boscho87/d5834ac1ba42aa3da02e905aa346ee30

Phalcon PHP framework not passing parameters to controller

Just started development in the Phalcon PHP framework and am also pretty new to PHP in general. My question is on how to create a request with a route, which I believe I have done, and pass the parameters of the route to the controller action that is linked to the said route. Below I have included the three files that I have been working on and summarize what each one is supposed to do. I also have the end result and where my problem lies directly.
The first file is the index.php file that takes in all route requests for my site.
<?php
//Include all routes on site
foreach (glob("../app/routes/*.php") as $filename)
{
include $filename;
}
foreach (glob("../app/controllers/*.php") as $filename)
{
include $filename;
}
//Create routes and initialize routes
$router = new \Phalcon\Mvc\Router();
$router->mount(new PublicRoutes());
$router->mount(new ApiRoutes());
$router->mount(new AdminRoutes());
$router->handle();
$controller = $router->getControllerName();
$action = $router->getActionName();
$params = $router->getParams();
$di = new \Phalcon\DI\FactoryDefault();
$d = new Phalcon\Mvc\Dispatcher();
$d->setDI($di);
$d->setControllerName($router->getControllerName());
$d->setActionName($router->getActionName());
$d->setParams($router->getParams());
$controller = $d->dispatch();
The second file is the actual routes mounted in for my API call which I am testing everything out with.
<?php
class ApiRoutes extends Phalcon\Mvc\Router\Group
{
public function initialize()
{
//Basic api route for pixelpusher
$this->add(
"/addhawk/api/:action/:model/:params",
array(
"controller" => "api",
"action" => 1,
"model" => 2,
"params" => 3,
)
);
}
}
The third, and final file is the controller class for the API with the only action I am testing right now.
<?php
class ApiController extends \Phalcon\Mvc\Controller
{
public function handlerAction()
{
//Pull in parameters
echo "<h1>API Handler Entered</h1>";
$model = $this->dispatcher->getParam("model");
echo $model;
//Choose correct api based off of api param
if( $model == "grid" ) {
echo 'grid';
}
else if ( $model == "admin" ) {
echo 'admin';
}
else {
//No valid api must have been found for request
}
//Return result from api call
return true;
}
}
So, the url is "localhost/addhawk/api/handler/grate/view" which results in the following output in html courtesy of line 9 in the ApiController.
There is no print out of the $model variable as it should do. There is also no error so I have no idea why it's not printing. According to the documentation and every resource I have read online, all parameters should be available directly from each controller action thanks to the dispatcher and $di class or something similar. So my question is why can I not access the parameters if everything seems to be saying I should be able to?

Dynamically call a method from a dynamically called class

I'm trying to call a controller and method pulled from the URI site.com/controller/method
Here is my current working code:
$__REQUEST__ = new URI_Request($_SERVER["REQUEST_URI"]);
$this->prepareController($__REQUEST__);
if($this->checkClass()) {
$this->controller = new $this->controller();
if($this->checkMethod($__REQUEST__)) {
$method = $__REQUEST__->getMethod();
$this->method = $method;
$this->controller->$method();
}
}
However, I want this line
$this->controller->$method();
To work as similarly to this
$this->controller = new $this->controller();
//achieves something like $this->controller = new IndexController();
//if the URL was something like site.com/index/test (/index/ gets manipulated)
i.e. something like
$this->controller->$this->method
I can see why this wouldn't work, however - is there a way to chain this or get it to reference the $method variable from the object characteristics rather than a stray variable?
$this->controller->{$this->method}();

Categories