I have a smarty project, in a php file, I set a abc variable:
$smarty->assign('abc', '123');
now I can use the {$abc} in its corresponding .tpl file.
but now can I use the abc in php file?
I tried $smarty->abc and $smarty->$abc all can not access.
EDIT-01
If I use
$smarty->assign('abc', '1234');
$abc2 = $smarty->get_template_vars('abc');
I will get bellow error:
Fatal error: Uncaught Error: Using $this when not in object context in /Users/sof3/Desktop/smarty-test02/php/test.php:36 Stack trace: #0 {main} thrown in /Users/sof3/Desktop/smarty-test02/php/test.php on line 36
If your Smarty version is version 2. you can use:
$smarty->get_template_vars('abc')
but if your Smarty version is 3, you should use:
$smarty->getTemplateVars('abc')
more detail refer to: https://www.smarty.net/docs/zh_CN/language.syntax.functions.tpl
Related
I'm trying to use MONGODB in my project and I just started using it when it has this problem. I created another directory and ran the files with no problems whatsoever. What is wrong with this? I keep getting Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() error message.
This is my code to the class called Database which is producing the error.
<?php
namespace auth;
include_once dirname(__DIR__) . "/config.php";
class Database
{
public function __construct(
private readonly string $dbname,
){
$this->run();
}
private function run(): void
{
$collection = (new \MongoDB\Client)->{$this->dbname};
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => 'admin#example.com',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
}
}
and my test.php file just contained some lines of code
include_once "./assets/php/config.php";
$database = new \auth\Database("test->users");
I definitely loaded the class MongoDB and my config.php includes the vendor file of composer's.
This is the error in full.
Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PP\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PPa\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
Process finished with exit code 255
The config.phpfile that I have included
include_once dirname(__DIR__, 2) . "/vendor/autoload.php";
include_once "autoload.php";
Thanks for any help.
Ah yes answer to my question, I'm just well not thinking properly at the time of writing the code. The `MongoDB\Collection" class accepts two arguments from the user and one internally set. The user provided argument is the database name and collection name. Doing it with (new \MongoDB\Client)->{$this->dbname} is wrong because it provides just the database string and not the collection name.
I resolved this issue by changing how the structure looks of the collection. md is just the alias for MongoDB class as I do not want to type such a long string everytime.
$collection = (new md\Client)->$db->$collection_name;
However, I have no idea how mongoDB uses the db and collection_name as magic constants as I have looked through the entire MongoDB\Client file and found there is no code stating those "magic constants"? I would appreciate if someone has an answer to this.
I've been struggling with this all day. I got brought in to a new app and they require php-cs-fixer to be ran on all files. The project has a .php_cs file in it, but I couldn't get it work in sublime text 3 nor could I get vscode to do it.
The command runs but I get the error:
PHP Fatal error: Uncaught Error: Class 'Symfony\CS\Config\Config' not found in C:\dev\code\.php_cs:63
Stack trace:
#0 C:\Users\Alex\AppData\Roaming\Composer\vendor\friendsofphp\php-cs-fixer\src\Console\ConfigurationResolver.php(933): include()
#1 C:\Users\Alex\AppData\Roaming\Composer\vendor\friendsofphp\php-cs-fixer\src\Console\ConfigurationResolver.php(228): PhpCsFixer\Console\ConfigurationResolver::separatedContextLessInclude('c:\\dev\\code\\Tao...')
#2 C:\Users\Alex\AppData\Roaming\Composer\vendor\friendsofphp\php-cs-fixer\src\Console\ConfigurationResolver.php(625): PhpCsFixer\Console\ConfigurationResolver->getConfig()
#3 C:\Users\Alex\AppData\Roaming\Composer\vendor\friendsofphp\php-cs-fixer\src\Console\ConfigurationResolver.php(458): PhpCsFixer\Console\ConfigurationResolver->getFormat()
#4 C:\Users\Alex\AppData\Roaming\Composer\vendor\friendsofphp\php-cs-fixer\src\Console\Command\FixCommand.php(151): PhpCsFixer\Console\ConfigurationResolver->getReporter()
#5 C:\Users\Alex\AppData\Roaming\Composer\vendor\symfony in C:\dev\code\.php_cs on line 63
Here is the .php_cs file
<?php
$finder = Symfony\Component\Finder\Finder::create()
->files()
->in(__DIR__)
->exclude('vendor')
->exclude('resources/views')
->exclude('storage')
->exclude('public')
->notName("*.txt")
->ignoreDotFiles(true)
->ignoreVCS(true);
$fixers = [
'-psr0',
'-php_closing_tag',
'blankline_after_open_tag',
'double_arrow_multiline_whitespaces',
'duplicate_semicolon',
'empty_return',
'extra_empty_lines',
'include',
'join_function',
'list_commas',
'multiline_array_trailing_comma',
'namespace_no_leading_whitespace',
'no_blank_lines_after_class_opening',
'no_empty_lines_after_phpdocs',
'object_operator',
'operators_spaces',
'phpdoc_indent',
'phpdoc_no_access',
'phpdoc_no_package',
'phpdoc_scalar',
'phpdoc_short_description',
'phpdoc_to_comment',
'phpdoc_trim',
'phpdoc_type_to_var',
'phpdoc_var_without_name',
'remove_leading_slash_use',
'remove_lines_between_uses',
'return',
'self_accessor',
'single_array_no_trailing_comma',
'single_blank_line_before_namespace',
'single_quote',
'spaces_before_semicolon',
'spaces_cast',
'standardize_not_equal',
'ternary_spaces',
'trim_array_spaces',
'no_useless_else',
'unalign_equals',
'unary_operators_spaces',
'whitespacy_lines',
'multiline_spaces_before_semicolon',
'short_array_syntax',
'short_echo_tag',
'concat_with_spaces',
'ordered_use',
];
return Symfony\CS\Config\Config::create()
->fixers($fixers)
->finder($finder)
->setUsingCache(true);
I can run the file manually by going to the cmd prompt and running
php-cs-fixer fix .\Controller.php
And it does what it's supposed to, I think with a default .php_cs file.
Driving me bats. :)
Problem is we were trying to use an older version of php-cs-fixer and it didn't function correctly with our version of php.
Fix was to update to php-cs-fixer v2.*
I was trying to compress PDF. I try my best to find out some library that can be free too for some limited purpose. I found out iLovePDF Library.
I try to get it done using composer and without composer too But not find any way to resolve it.
My Code:
<?php
require_once('vendor/autoload.php');
// require_once('vendor/ilovepdf/init.php');
$ilovepdf = new Ilovepdf('project_public_key','secret_key');
$myTask = $ilovepdf->newTask('compress');
$file1 = $myTask->addFile('file1.pdf');
$myTask->execute();
$myTask->download();
?>
Fatal error: Uncaught Error: Class 'Ilovepdf' not found in C:\xampp\htdocs\PHP Doc\E15\index.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\PHP Doc\E15\index.php on line 11
You need to use namespaces:
$ilovepdf = new \Ilovepdf\Ilovepdf('project_public_key','secret_key');
Or:
use \Ilovepdf\Ilovepdf;
$ilovepdf = new Ilovepdf('project_public_key','secret_key');
Thank You.
I resolved it by adding below line at the top of index.php
namespace Ilovepdf;
Anyone can help with this issue:
I have simplesamlphp instaled and tested successfully with my IdP and SP.
when I integrated the APP following the doc (step 6) https://simplesamlphp.org/docs/1.5/simplesamlphp-sp I started getting errors I will post bellow.
Here is my autoload.php:
require __DIR__.'/../vendor/autoload.php';
require_once('/var/simplesamlphp/lib/_autoload.php');
$as = new SimpleSAML_Auth_Simple ( 'default-sp' );
if ($as->isAuthenticated ()) {
//die ( 'ok' );
} else {
$param = array (
'ReturnTo' => 'http://10.128.240.181/'
);
$as->requireAuth ( $param );
}
$attributes = $as->getAttributes();
print_r($attributes);
$session = SimpleSAML_Session::getSessionFromRequest();
$session->cleanup();
Error output:
Feb 07 16:52:32 simplesamlphp WARNING [CL0a04f709] The class or interface 'SimpleSAML_Auth_Simple' is now using namespaces, please use 'SimpleSAML\Auth\Simple'.
PHP Notice: Undefined index: REQUEST_URI in /var/simplesamlphp/lib/SimpleSAML/Utils/HTTP.php on line 810
PHP Stack trace:
PHP 1. {main}() /var/www/Cachet/bootstrap/autoload.php:0
PHP 2. SimpleSAML\Auth\Simple->requireAuth() /var/www/Cachet/bootstrap/autoload.php:58
PHP 3. SimpleSAML\Auth\Simple->login() /var/simplesamlphp/lib/SimpleSAML/Auth/Simple.php:103
PHP 4. sspmod_saml_Auth_Source_SP->initLogin() /var/simplesamlphp/lib/SimpleSAML/Auth/Simple.php:161
PHP 5. sspmod_saml_Auth_Source_SP->authenticate() /var/simplesamlphp/lib/SimpleSAML/Auth/Source.php:193
PHP 6. sspmod_saml_Auth_Source_SP->startSSO() /var/simplesamlphp/modules/saml/lib/Auth/Source/SP.php:431
PHP 7. sspmod_saml_Auth_Source_SP->startSSO2() /var/simplesamlphp/modules/saml/lib/Auth/Source/SP.php:336
PHP 8. sspmod_saml_Auth_Source_SP->sendSAML2AuthnRequest() /var/simplesamlphp/modules/saml/lib/Auth/Source/SP.php:298
PHP 9. SAML2\HTTPRedirect->send() /var/simplesamlphp/modules/saml/lib/Auth/Source/SP.php:314
PHP 10. SAML2\Compat\Ssp\Container->redirect() /var/simplesamlphp/vendor/simplesamlphp/saml2/src/SAML2/HTTPRedirect.php:83
PHP 11. SimpleSAML_Utilities::redirectTrustedURL() /var/simplesamlphp/vendor/simplesamlphp/saml2/src/SAML2/Compat/Ssp/Container.php:52
PHP 12. SimpleSAML\Utils\HTTP::redirectTrustedURL() /var/simplesamlphp/lib/SimpleSAML/Utilities.php:276
PHP 13. SimpleSAML\Utils\HTTP::normalizeURL() /var/simplesamlphp/lib/SimpleSAML/Utils/HTTP.php:968
PHP 14. SimpleSAML\Utils\HTTP::getSelfURL() /var/simplesamlphp/lib/SimpleSAML/Utils/HTTP.php:889
****PHP Fatal error: Uncaught SimpleSAML_Error_UnserializableException: Unable to parse base url: http://localhost in /var/simplesamlphp/lib/SimpleSAML/Auth/Source.php:197****
Stack trace:
#0 /var/simplesamlphp/lib/SimpleSAML/Auth/Simple.php(161): SimpleSAML_Auth_Source->initLogin('http://10.128.2...', NULL, Array)
#1 /var/simplesamlphp/lib/SimpleSAML/Auth/Simple.php(103): SimpleSAML\Auth\Simple->login(Array)
#2 /var/www/Cachet/bootstrap/autoload.php(58): SimpleSAML\Auth\Simple->requireAuth(Array)
#3 {main}
thrown in /var/simplesamlphp/lib/SimpleSAML/Auth/Source.php on line 197
Anyone has an idea what could be the cause of this error ? I am a newbie in php and SAML,
I think there are two issues. The first one is:
Feb 07 16:52:32 simplesamlphp WARNING [CL0a04f709] The class or interface 'SimpleSAML_Auth_Simple' is now using namespaces, please use 'SimpleSAML\Auth\Simple'.
It seems you are following an old documentation and using a recent version of SimpleSAMLphp. You will have to use $as = new \SimpleSAML\Auth\Simple('default-sp'); instead of $as = new SimpleSAML_Auth_Simple ( 'default-sp' );
If you are using the latest stable version of SimpleSAMLphp, please follow https://simplesamlphp.org/docs/stable/simplesamlphp-sp
The second issue is
****PHP Fatal error: Uncaught SimpleSAML_Error_UnserializableException: Unable to parse base url: http://localhost in /var/simplesamlphp/lib/SimpleSAML/Auth/Source.php:197****
This is more related to the configuration (This is a wild guess). Can you provide more info on your web server (IIS/Apache/nginx)? I think IIS has some known issues: https://github.com/simplesamlphp/simplesamlphp/issues/540
As adviced in Smarty's instruction, in order to use deprecated functions like include_php in newest PrestaShop, I had to switch from Smarty.class.php to SmartyBC.class.php. I did it by modyfing in smarty.config.inc.php following lines:
require_once(_PS_SMARTY_DIR_.'SmartyBC.class.php');
// require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
global $smarty;
// $smarty = new Smarty();
$smarty = new SmartyBC();
However, using {include_php file='./custom_php/manufacturers.php'} in theme's header.tpl still results in blank screen. Errors from php_error_log :
[22-Jul-2014 15:05:55 Europe/Warsaw] PHP Notice: Undefined property: SmartyBC::$trusted_dir in C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\Smarty.class.php on line 676
[22-Jul-2014 15:05:55 Europe/Warsaw] PHP Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\themes\trzmiel\header.tpl" on line 101 "{include_php file='./custom_php/manufacturers.php'}" {include_php} file './custom_php/manufacturers.php' is not readable' in C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php:667
Stack trace:
#0 C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_compile_include_php.php(81): Smarty_Internal_TemplateCompilerBase->trigger_template_error('{include_php} f...', 101)
#1 C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php(485): Smarty_Internal_Compile_Include_Php->compile(Array, Object(Smarty_Internal_SmartyTemplateCompiler), Array, NULL, NULL)
#2 C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templ in C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php on line 667
It seems that trusted_dir property is available only when using Security Policy:
class My_Security_Policy extends Smarty_Security {
public $trusted_dir = './custom_php/';
}
$smarty = new SmartyBC();
// enable security
$smartyBC->enableSecurity('My_Security_Policy');
so you should provide here correct path (I don't know is it relative or not - you should give a try) and then you should be able to use manufacturers.php file in your template file but you should also here look at correct file path.