Yii2 console command pass arguments with name - yii2

I want to use commands as
php yii sync anyvar2=anValue anyVar1=anyValue
In controller
public function actionIndex(){
echo $anyVar1;
echo $anyVar2;
}
I tried with php yii sync [--anyvar2=anValue ,--anyVar1=anyValue]

1) If you want to set controller parameters:
class SyncController extends \yii\console\Controller
{
public $anyVar1;
public $anyVar2;
public function options($actionID)
{
return array_merge(parent::options($actionID), [
'anyVar1', 'anyVar2'
]);
}
}
Now you can set them like that:
php yii sync --anyVar1=aaa --anyVar2=bbb
2) If you want to just pass variables as arguments:
public function actionIndex($anyVar1, $anyVar2)
{
// ...
}
Now you can set them like that:
php yii sync aaa bbb

Got solution
when need to pass variable in console
Variable should declared in public scope.
Variable should returned in options function
Ex:
class SyncController extends \yii\console\Controller
{
public $anyVar1;
public $anyVar2;
public function options()
{
return ['anyVar1','anyVar2'];
}
public function actionIndex(){
echo $this->anyVar1."\n";
echo $this->anyVar2."\n";
}
}
In console
php yii sync --anyVar2=1111 --anyVar1=999

Related

Yii2 Basic calling a function from another file into controller

I create a file in yii2 "actions", and I create a class name actionC
is it possible to call function from
actions/actionC
inside a controller
my calss is
<?php
namespace app\actions;
class ActionC
{
protected function CPost(){
// return something
}
}
is it possible to call my function CPost() inside a controller actionView ?
Hello while your function is protected you can not call it, if you want to call a function of your class it has to be public it would be something like that
Class actionC
<?php
namespace app\actions;
class ActionC
{
protected function CPost()
{
// return something
}
public function BPost()
{
// return something
}
}
And in you View
<?php
$a=new \app\actions\ActionC();
$a->BPost();
//$a->CPost(); //this will be error because is protected

Passing a variable data in all controllers in codeigniter

Iam developing a website using codeigniter.I want to pass a particular data to all the controller.I searched and find it can be done by session like below.
$var = $this->session->userdata('passingdata');
But this data loses when history is cleared and when session expires.
Is there any other way to pass variable to all controllers.
You can try this
- create MY_Controller in application/core. it looks like
Class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('HomeModel');
$this->global_data['settings']= $this->HomeModel->getSettings();
}
function display_view($view, $local_data = array()) {
$data = array_merge($this->global_data, $local_data);
return $this->load->view($view, $data);
}}
then extend this controller in your all controller
your functions in your controller like
Class Home extends MY_Controller {
public function index() {
$data['title'] = 'test';
$this->display_view('home', $data);
}
}

Laravel 4: (routing?) to search keyword url

Let's say for example in results q='sports', how would I be able to make it www.example.com/results?q=sports or whatever? And when I type manually in the url www.example.com/results?q=books it would search for books.
The response is in json btw.
You will need to process the parameter from within whatever controller '/results' is routed to, eg:
if isset($_GET['q']) {
$searchCategory = $_GET['q'];
}
Another option would be to have q as a function parameter on a controller method:
class MyController extends BaseController {
public function __construct() {}
public function getSearch($searchCategory) {
// Do Stuff
}
}
And this would be accessible via:
http://www.example.com/search/books

Zend framework 2: How to get custom config from model?

In my application, there are custom configs and I want to get them into the model.
I read about one way, but it can not perform:
namespace Core\Model;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\TableGateway\Feature\FeatureSet;
use Zend\Db\TableGateway\Feature\GlobalAdapterFeature;
use Zend\Db\Sql\Delete,
Zend\Db\Sql\Insert,
Zend\Db\Sql\Update,
Zend\Db\Sql\Select;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class BaseModel extends AbstractTableGateway implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}
public function getServiceLocator() {
return $this->serviceLocator;
}
public function __construct()
{
$this->featureSet = new FeatureSet();
$this->featureSet->addFeature(new GlobalAdapterFeature());
$this->initialize();
}
}
In the model I prescribe
$config = $this->getServiceLocator()->get('config');
or
$config = $this->getServiceLocator();
but the result = NULL
Who can tell what I'm doing wrong?
You have to create instances of your classes that extend BaseModel using the ServiceManager. If you use new, then you have to set the ServiceManager yourself.

Calling a function from within that class - php

I have a function called wsGO() inside a class ceff(). I want to be able to run that function from within the class ceff. I used this code:
class ceff{
$ceff_instance = new ceff($this);
$ceff_instance = wsGO();
public function wsGO(){
....
}
}
However this didn't work. Any ideas?
In PHP you can only initialize a property with a constant - NOT functions or constructor calls. You can though do that in a constructor, such as:
class ceff{
public $ceff_instance;
public function __construct() {
$this->ceff_instance = $this->wsGO();
}
public function wsGO(){
....
}
}