I m trying to use the method name from variable as below but however can't getting success in it. code is as below
$function_name = $this->input->post('class_name');
if(method_exists($this, $function_name))
{
$foo = $this;
$foo->$function_name();
}
else
{
show_404();
}
But keep getting the error of Method name must be a string
Fatal error: Call to undefined method trialdemo::() in D:\xampp\htdocs\trialdemo\application\controllers\trialdemoapp.php on line 38
Try this
if(method_exists($this, $function_name))
{
call_user_func($function_name);
}
else
...
In addition to call_user_func you might want to read about call_user_func_array and is_callable.
Related
Is this:
$paginate = $request->get('paginate');
Equivalent to this, for getting a query param if it is present or assign to the associated variable "null" it it is not present:
if ($request->has('paginate')) {
$paginate = $request->get('paginate');
} else {
$paginate=null;
}
According to get() method documentation:
This method belongs to Symfony HttpFoundation and is not usually needed when using Laravel.
Alternatively you can use filled and $request->paginate
So it checks if the request has the "item"and it has value.
$paginate = null;
if ($request->filled('paginate')){
$paginate = $request->paginate;
}
First of all, I'm quite new to Magento 2, but I've used Magento 1.x for some time.
I've read a lot about how to solve DI-related problems, but I'm stuck on this one:
Exception #0 (Exception): Recoverable Error: Argument 1 passed to Cefar\AO\Helper\Ao::__construct() must be an instance of Magento\Framework\App\Helper\Context, instance of Magento\Framework\ObjectManager\ObjectManager given, called in .../vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in .../Cefar/AO/Helper/Ao.php on line 11
Many other answers have suggested deleting the var/di and var/generation folders, sometimes var/cache also. While this solves the problem, it occurs again once bin/magento setup:di:compile is run, which means the code cannot be used in a production environment.
I've checked that the Ao class does not instantiate any objects. It also doesn't try to re-make any objects that could be provided by the context given. Here's the code:
namespace Cefar\AO\Helper;
class Ao extends \Magento\Framework\App\Helper\AbstractHelper
{
const DEFAULT_GRID_COLS = 4;
protected $_session;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Customer\Model\Session $session
)
{
parent::__construct($context);
$this->_session = $session;
}
public function getConfig($path)
{
return $this->scopeConfig->getValue($path);
}
public function isActive($url = null, $print = true) {
$active = ($url && strstr($_SERVER['REQUEST_URI'], $url) !== false);
if ($active && $print) {
echo "active";
} else {
return $active;
}
}
public function isLoggedIn()
{
return $this->_session->isLoggedIn();
}
public function limitWords($text = '', $limit = 10, $showDots = true)
{
$words = explode(' ', $text);
$limited = array_slice($words, 0, $limit);
$newText = implode(' ', $limited);
if (count($words) > $limit && $showDots) {
$newText .= '...';
}
return $newText;
}
public function getCurrentGrid()
{
return ($this->_getRequest()->getParam('grid'))
? $this->_getRequest()->getParam('grid')
: self::DEFAULT_GRID_COLS;
}
}
There's nothing particularly special here. I'm confused as to how this is even happening; every other defined class in the extension is getting its DI parameters correctly. Why is the ObjectManager apparatus providing an unwanted argument? The relevant call is given in the error report as:
.../vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93): Cefar\AO\Helper\Ao->__construct(Object(Magento\Framework\ObjectManager\ObjectManager))
So it isn't even providing two arguments!
I've also read about providing type hints in a di.xml, but it doesn't seem to be relevant here as both types are part of the Magento libraries? I note that there is an entry for Magento\Framework\App\Helper\Context but not one for Magento\Customer\Model\Session... but that there are framework classes that use ID to import Magento\Customer\Model\Session already which work.
Long story short, this was because of a typo.
Sometimes when the helper was being included, it was being referred to as Cefar\AO\Helper\Ao, and other times, Cefar\AO\Helper\AO. Essentially, the ObjectManager was resolving both of these references to the same class, but it only had type hints for one of the names so it didn't know what to provide to the incorrect one.
A little help would have been nice, Magento! Maybe an error report that the requested class wasn't found? Still, at least this is finally over with.
whats wrong with my code ?
$count=$this->db_ecommerce->check_user($user, $pswd)->num_rows(); <-- this line 43
$user=$this->input->post('username');
$pswd=$this->input->post('password').$this->config->item("key_login");
//$pswd=$this->input->post('password');
$count=$this->db_ecommerce->check_user($user, $pswd)->num_rows();
if($count>0) {
$this->session->set_flashdata('status_login', 'ok');
redirect('member');
before you get the num_rows() you should check if there is any result:
$count = $this->db_ecommerce->check_user($user,$pswd);
if($count){
$count = $count->num_rows();
}
else{
$count = 0;
}
Assuming that you have already a model with db_ecommerce as name, you should try loading it :
$this->load->model('db_ecommerce');
$count=$this->db_ecommerce->check_use ...
I have a numeric parameter in the symfony's parameters.yml file and I have a configuration class in my bundle where I validate that the parameter is an integer, but I get the following error in the validation proccess:
[Symfony\Component\Config\Definition\Exception\InvalidTypeException]
Invalid type for path "page_size". Expected int, but got string.
Is there a way to specify the parameter type in the parameters.yml file?
EDIT
parameters.yml:
parameters:
...
list_page_size: 15
...
config.yml:
...
example:
page_size: %list_page_size%
...
Configuration.php:
class Configuration implements ConfigurationInterface
{
/**
* {#inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('example');
$rootNode->children()
// Some code here
->integerNode('page_size')
->defaultValue(15)
->end()
// More code here
->end();
return $treeBuilder;
}
}
EDIT 2
I have discovered that the exception is thrown in the prepend() method in the bundle extension file (DependencyInjection/ExampleExtension.php), but the error is not thrown in the load method of the same file. It seems like if the parameters.yml file was not loaded at prepend method execution time.
The exception is simply because the is_int check fails.
A simple solution would be to add a normalization step like:
// Some code here
->integerNode('page_size')
->beforeNormalization()
->ifString()
->then(function ($val) { return strval(intval($val)) === $val ? intval($val) : $val; })
->end()
->defaultValue(15)
->end()
// More code here
Also take a look at http://symfony.com/doc/current/components/config/definition.html#normalization
If you entered something like :
page_size: "10"
Then it's a normal behavior.
You should not use " :
page_size: 10
With the new validator object - is it possible to replace the validation error inside the validation rule triggered? to not only return the static error message but maybe some dynamically genereted one?
public function validateLength($data) {
...
$length = mb_strlen($data['name']);
$this->validator()->getField('name')->setRule('validateLength', array('message' => $length . 'chars'));
...
}
does not work, of course (too late I guess)
I want to actually return the lenght of the string (you used 111 chars from 100 allowed) for example - but for this I would need to be able to replace the message from inside the (custom) validation method
$this->validate['name']['validateLength']['message'] = $length . 'chars';
also never worked so far. it would always return the previous (static) error message from the $validate array
public function customValidator($data) {
....
if ($validates) {
return true;
} else {
return 'my new error message';
}
}
The following snippet should do the trick:
public function validateLength($data) {
...
$length = mb_strlen($data['name']);
$this->validator()->getField('name')->getRule('validateLength')->message = $length . 'chars';
...
}