The desired result is:
class SomeExtension extends Extension{
public static function add_to_class($class, $extensionClass, $args = null){
//$args = ["some_key"=>"some value"];
}
}
My config.yml is (incorrectly formatted) like so:
SomeClass:
extensions:
- SomeExtension
some_key: "some value"
This yml doesn't parse, but is it possible to inject args into an extension like this? I can't find any documentation on how to use the 3rd parameter of Extension::add_to_class
You can pass the arguments in parentheses, like so:
SomeClass:
extensions:
- "SomeExtension('arg1','arg2')"
Related
I'm using Knp SnappyBundle in Symfony 4.
I'm having trouble passing the custom-header argument (--custom-header <name> <value>) to wkhtmltopdf through yaml or as option in request.
Here is what I'm trying, which seems to fail:
knp_snappy:
temporary_folder: "%kernel.cache_dir%/snappy"
pdf:
enabled: true
binary: xvfb-run wkhtmltopdf
options:
- { name: 'custom-header', value: '%app_auth_header_name%' '%app_auth_header_token%' }
I've also tried passing the values as an array, but that also fails.
I have solved it partially, unfortunately not by yaml, but direcly in code example:
public function getPdfBinary($url, Pdf $pdfService): PdfResponse
{
$url = urldecode($url);
$res = new PdfResponse($pdfService->getOutput($url, ['custom-header' =>
[ 'X-Authorization' => 'mytoken' ] ]),'output.pdf');
return $res;
yaml expect scalar value of custom-header...
I want to write an integer value from a controller to parameters.yaml. Is that even possible?
Example:
parameters.yaml
parameters:
# ...
counter: 13
SomeController.php
class SomeController
{
public function indexAction()
{
$counter = $this->getParameter('counter');
$counter++;
// now save new counter value to parameters.yaml !??
}
}
Parameters are generally fixed values.
So A better approach is probably writing into an individual yaml file:
http://symfony.com/doc/current/components/yaml/introduction.html
use Symfony\Component\Yaml\Dumper;
const MY_PARAM=13;
//manipulate and do your thing....
$array=['my_param'=>self::MY_PARAM++];
$dumper = new Dumper();
$yaml = $dumper->dump($array);
file_put_contents('/path/to/file.yml', $yaml);
Then you read the file wherever you need it in your application.
use Symfony\Component\Yaml\Parser;
$yaml = new Parser();
$value = $yaml->parse(file_get_contents('/path/to/file.yml'));
Parameters.yml must contain only fixed configuration values ! You should store your counter in database or (i don't like this) in txt file.
But if you really want to edit it. You have to parse the file and search / replace the line ... It's really a bad practice !
I need to create classes based on the parameter passed to a function. I do it this way:
public function index($source)
{
if(in_array($source, ModuleManager::getAllModules()))
{
$provider = new $source();
if($image)
{
return $provider->getAll(true);
}
else
{
return $provider->getAll(false);
}
}
}
Notice that on line 5 I'm trying to create an object of class $source which will definitely be available. I understand that the above code is actually an eval call. I'm using Laravel 5.2 and the above code returns:
FatalThrowableError in ProcReqController.php line 19:
Fatal error: Class 'Example' not found
In the above error Example can be any class that I made. Now if I hard code the value of $source then it works just fine.
What am I getting that error?
I believe what's happening is PHP gets confused when you try to instantiate a class whose class name is in a variable and it has to do with imports.
Solution 1
Set your $class variable to the fully qualified class name including the namespace and it should work.
In this way, new $class() should work even while including parenthesis.
Solution 2
After further testing, it seems when you instantiate a variable class, it always assumes global namespace.
With this in mind, you can use class_alias to alias each of your classes. In config/app.php, you can add each class to the aliases array.
'aliases' => [
....
'Example' => App\Example::class
]
The autoloader allows you to use classes without fully qualifying them... in the php interactive shell you'll have to manually include classes AND fully qualify them.
if you have a composer project, go to it's directory and do the following to load the Primal color classes:
set_include_path(getcwd().'/vendor/primal/color/lib/Primal/Color/');
include 'Color.php';
include 'Parser.php';
include 'RGBColor.php';
include 'HSVColor.php';
$hello = Primal\Color\Parser::parse('#666');
var_export($hello->toHSV());
/*
returns
Primal\Color\HSVColor::__set_state(array(
'hue' => 0,
'saturation' => 0,
'value' => 37.647058823529413,
'alpha' => 1,
))
*/
Remove the parentheses at the end of the instantiation call, I think.
Check out this php interactive shell session:
php > class Foo { };
php > $fooname = 'Foo';
php > $bar = new $fooname;
php > var_dump($bar);
object(Foo)#2 (0) {
}
src: https://stackoverflow.com/a/4578350/2694851
I`m using zend framework and my urls are like this :
http://target.net/reward/index/year/2012/month/11
the url shows that I'm in reward controller and in index action.The rest is my parameters.The problem is that I'm using index action in whole program and I want to remove that part from URL to make it sth like this :
http://target.net/reward/year/2012/month/11
But the year part is mistaken with action part.Is there any way ?!!!
Thanks in advance
Have a look at routes. With routes, you can redirect any URL-format to the controller/action you specify. For example, in a .ini config file, this will do what you want:
routes.myroute.route = "reward/year/:myyear/month/:mymonth"
routes.myroute.defaults.controller = reward
routes.myroute.defaults.action = index
routes.myroute.defaults.myyear = 2012
routes.myroute.defaults.mymonth = 11
routes.myroute.reqs.myyear = "\d+"
routes.myroute.reqs.mymonth = "\d+"
First you define the format the URL should match. Words starting with a colon : are variables. After that you define the defaults and any requirements on the parameters.
you can use controller_plugin to control url .
as you want create a plugin file (/library/plugins/controllers/myplugin.php).
then with preDispatch() method you can get requested url elements and then customize that for your controllers .
myplugin.php
class plugins_controllers_pages extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$int = 0;
$params = $this->getRequest()->getParams();
if($params['action'] != 'index' ) AND !$int)
{
$int++;
$request->setControllerName($params['controller']);
$request->setActionName('index');
$request->setParams(array('parameter' => $params['action']));
$this->postDispatch($request);
}
}
}
I have the latest codeigniter version, and was wondering how can i get the segments in a url by their parameter name. For instance, here is a sample url:
www.somewebsitedomain.com/param1=something/param2=somethingelse/
now lets say i want to get the value for 'param1', which is 'something', how would i do so using the uri class?
Because by default the uri class only gets segments by number, or the order in which they appear, but i want to get segments by the parameter name in that segment. Or in general just get the parameter value. Hope that makes sense...
You could do $this->uri->uri_to_assoc(n) which will give something like the following
[array]
(
'name' => 'joe'
'location' => 'UK'
'gender' => 'male'
)
Then just just the param name you would like.
Source: http://codeigniter.com/user_guide/libraries/uri.html
You could actually put them as GET vars and use the Input Class:
$param = $this->input->get('param1'); // something
or you can do:
$params = $this->input->get(); // array('param1' => 'something', 'param2' => 'somethingelse')
to get all the parameters
I'm not sure this is what you're asking, but in many applications, URLs follow the form www.domain.com/controller/method/key1/value1/key2/value2. So I decided to extend the CI_URI class to return the value of any "key".
If you put this in your application/core folder, in a file named, MY_URI.php, it extends the built-in URI class:
class MY_URI extends CI_URI {
/* call parent constructor */
function __construct() {
parent::__construct();
}
/* return value of the URI segment
which immediately follows the named segment */
function getNamed($str=NULL) {
$key = array_search($str, $this->segments);
if ($key && isset($this->segments[$key+1])) {
return $this->segments[$key+1];
}
return false;
}
}
Then if your URLs are in the form
www.somewebsitedomain.com/param1/something/param2/somethingelse/
you can call this function as follows:
$this->uri->getNamed('param1) - returns "something"
$this->uri->getNamed('param2) - returns "somethingelse"