I've one module -> users.
->modules
->users
->controllers
->models
->views
->Users.php
I created one 'config.php' inside 'config' folder of 'users' modules.
->modules
->users
->config
->config.php
->controllers
-> List of Controllers
->models
-> List of models
->views
-> List of Views
->Users.php
And, i gave directory path of config.php in init() method of Users.php, as
modules/users/Users.php
<?php
namespace app\modules\users;
class Users extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\users\controllers';
public $commonModel = 'app\modules\users\models\Users';
public function init()
{
parent::init();
\Yii::configure($this,require(__DIR__.'/config/config.php'));
}
}
But, it is giving error like
PHP Warning – yii\base\ErrorException
"Invalid argument supplied for
foreach()".
Screenshot
I am taking reference from Yii2.0 Guide to include a path inside init() method.
Please help me to rectify this issue.
Thanks.
From what I can see, you're passing in the PHP code to your config file, instead of passing in a configuration array
Instead of this...
\Yii::configure($this, require(__DIR__.'/config/config.php'));
Try doing this...
$config = require(__DIR__.'/config/config.php');
\Yii::configure($this, $config);
In your config.php file you should be returning an array, if you're using the basic-app config file and adding to that then it should be set up like this already
The problem comes from the configuration file. The configureation file must returns an array. Make sure that the config file is as follows:
<?php
$config = [
'name1' => 'value1',
'name2' => [/* something here */],
];
return $config;
you edit file model and delete code:
/**
* #inheritdoc
* #return DriverSearch the active query used by this AR class.
*/
public static function find()
{
return new DriverSearch(get_called_class());
}
Related
I want to set public static variable inside vendor folder.
I want to change this
public static $serverKey_as = 'my-secret-key';
into this, get the key from config -> app.php file
public static $serverKey_as = config('app.serverkey_as');
But i get this error
Symfony\Component\ErrorHandler\Error\FatalError: Constant expression contains invalid operations in file
this is my code in config -> app.php
'serverkey_as' => env('SERVERKEY_AS', 'my-defauly-secret-key'),
and this is my .env
SERVERKEY_AS = 'my-secret-key'
and this is what i've try but still no luck
<?php
namespace Midtrans;
class Config
{
public static $serverKey_as;
public function __construct()
{
return self::$serverKey_as = config('app.serverkey_as');
}
}
Got any hint for me?
First of all: you really shouldn't change code in the /vendor directory. That directory contains code made and maintained by other people.
Furthermore, an initial static variable assignment cannot contain function calls of any kind. I would suggest using the boot() method in your AppServiceProvider to change the static variable to the value you want:
public function boot()
{
\Midtrans\Config::$serverKey_as = config('app.serverkey_as');
//...
}
I have a widget that uses assets. The asset look like this:
class publicHeaderNavbarAsset extends AssetBundle {
public $sourcePath = '#app/components/#device';
public $css = [
'styles.css'
];
public $js = [];
public $depends = [];
}
I also have a (bootstrapped) component, defined like that:
class Aliases extends Component {
public function init() {
Yii::setAlias('#device', Utils::device());
}
}
Utils::device() is a function that parses the UA of the device and returns mobile, tablet or desktop, depending on the device type.
The problem is that Yii2 doesn't seem to be converting #device to the value it has. I first thought that it could be my fault, but then I changed the sourcePath to:
public $sourcePath = '#app/components/#app';
just to see if that will trigger an error with a duplicated path (basepath/componenets/basepath), but it didn't.
Is there a way I can change the sourcePath of my asset at runtime? Or maybe make Yii2 parse all the aliases in sourcePath?
Look at the getAlias function http://www.getyii.com/doc-2.0/api/yii-baseyii.html#getAlias()-detail
It will basically not match your second alias in the string you have given it.
You can try setting
Yii::setAlias('#device', '#app/components/' . Utils::device());
and
public $sourcePath = '#device';
This should work as you should be able to set an alias based on another alias
http://www.yiiframework.com/doc-2.0/guide-concept-aliases.html#defining-aliases
I have a folder named 'Validators' inside my 'Models' folder that contains the validation files for creating new records. I have not had problems in the past putting this folder inside the 'Models' folder within my Laravel PHP project but for some odd reason when I try to create/store new records, I keep getting a Class 'Models\Validators\Stone' not found error.
Controller:
<?php
use Acme\repositories\StoneRepository;
use Acme\repositories\PhotoRepository;
use Models\Stone;
use Models\Stone_Photo;
/* 'Validators' folder inside 'Models' folder */
use Models\Validators as Validators;
class StonesController extends BaseController {
/*The stone model */
protected $stone;
/*The stone_photo model */
protected $stone_photo;
protected $layout = 'layouts.master';
/* This is the function that is currently being called */
public function store()
{
$input = \Input::all();
/* This is where the error occurs on this line below */
$validation = new Validators\Stone;
/* Validation code here */
}
}
Stone Validator (app\models\validators\stone.php):
<?php namespace Models\Validators;
class Stone extends Validator {
/* The rules for validating the input */
public static $rules = array(
'stone_name' => 'required',
'stone_description' => 'max:255',
);
}
I have tried running 'php artisan dump-autoload' but that still does not change anything. This implementation has worked for me in the past but for some reason I keep getting this error and I don't know why. Any help is greatly appreciated!
I am using cakephp-2.x. I have one function name user_info() in the UsersController.php i want to access this in another controller name MessagesController.php
Code -
UsersController.php
public function user_info(){
$user_id=$this->Session->read('Auth.User.id');
$data=$this->User->findById($user_id);
$this->set('user_info',$data);
}
MessagesController.php
public function index(){
//$userInfo=new UsersController();
//$userInfo->user_info();
$this->user_info();
pr($data);
}
Error Message-
Fatal Error
Error: Call to undefined method MessagesController::user_info()
File: E:\xampp\htdocs\2014\myshowcam\msc\app\Controller\MessagesController.php
Line: 18
Notice: If you want to customize this error message, create app\View\Errors\fatal_error.ctp
Typically if you're trying to access a function in one controller from another controller you have a fundamental flaw in your project's logic.
But in general object usage is thus:
$otherController = new whateverMyControllerNameIs();
$otherController->functionName();
However I'm not familiar enough with cake to tell you the pitfalls of doing such a thing. For example I have no idea what this would do to routes or what other variables/objects are required to initialize a controller correctly.
EDIT:
Ref: CakePHP 2.3.8: Calling Another Controller function in CronController.php
App::import('Controller', 'Products'); // mention at top
// Instantiation // mention within cron function
$Products = new ProductsController;
// Call a method from
$Products->ControllerFunction();
Try requestAction function of cakephp
$result = $this->requestAction(array('controller' => 'users', 'action' => 'user_info'));
Why would a simple, When can complicated?
All the information for a registered user of User model is accessible in the following manner:
AppController.php
public $user_info; /* global scope */
public function beforeFilter(){
$this->user_info = $this->Auth->user(); // for access user data in any controller
$this->set('user_info_view',$this->Auth->user()); // for access user data in any view or layout
}
MessagesController.php
public function index(){
debug($this->user_info);
$my_messages = $this->Message->find('all',
array('conditions' => array('Message.user_id' => $this->user_info['id']))
}
....
layout or view.ctp
<?php echo $user_info_view['name']; ?> // email, etc
Why not take advantage of the way CakePHP handles relationships? There's a very easy way to achieve what you're trying to do without extending controllers or loading in additional controllers which seems excessive for your example.
Inside AppController's beforeFilter()
Configure::write('UserId', $this->Session->read('Auth.User.id'));
This will allow you to access the UserID from your models
Inside your User's model, create the following function
/**
* Sample query which can be expanded upon, adding fields or contains.
*
* #return array The user data if found
*/
public function findByUserId() {
$user = $this->find('first', array(
'conditions' => array(
'User.id' => Configure::read('UserId')
)
));
return $user;
}
Inside your Users controller (Minimal is better, no?)
public function user_info() {
$this->set('user', $this->User->findByUserId());
}
Inside your Messages controller
public function index() {
$this->set('user', $this->Message->User->findByUserId());
// --- Some more stuff here ---
}
And that's it, no need to be extending controllers, just make sure your Message and User model are related to each other, failing that you can bindModel or use ClassRegistry::init('User')-> for example.
I have poured over the documentation but I can't seem to find out how I can route a URI to a module.
My module currently contains a single controller using the correct directory structure (currently a ton of empty directories). I have my controller inside modules/module_name/classes/controller and my routes file inside modules/module_name/config/routes.php.
I have tried the following in both /app/config/routes.php and modules/module_name/config/routes.php:
<?php
return array(
'_root_' => 'md5_encrypt/index', // The default route
'tools/geek/md5_encrypt' => array('md5_encrypt'),
);
The controller looks like below (but I don't think that is relevant):
<?php
/**
* MD5 Encrypt Controller.
*
* Online tool to encrypt a string using MD5
*
* #package app
* #extends Controller
*/
namespace Md5_encrypt;
class Controller_Md5_Encrypt extends Controller_Template
{
/**
* The tool
*
* #access public
* #return Response
*/
public function action_index()
{
$data = array();
$this->template->tab = 'geek';
$this->template->title = 'MD5 Encrypt Tool';
$this->template->content = View::forge('welcome/index', $data);
}
}
You can't have an underscore in a namespace name. Same for the controller name.
The autoloader will convert underscores to directory separators when looking for the file.
First you should set the path of your application modules in app/config/config.php
'module_paths' => array(
APPPATH.'modules'.DS, // path to application modules
)
Second set routing in app/config/routes.php
'tools/geek/md5_encrypt' => 'md5_encrypt(module_name)/md5_encrypt(controller)',
However, since you are using underscore for your Controller's Name class Controller_Md5_Encrypt extends Controller_Template, it resulted to a new path.
/modules/md5_encrypt/classes/controller/md5/encrypt.php
Underscore(_) in your Controller's name was converted to a directory separator during autoloading http://fuelphp.com/docs/general/coding_standards.html#classes
Your /modules/md5_encrypt/classes/controller/md5_encrypt.php file was not found during autoloading.