use custom function in assetmanager - php

How use Yii::$app->session['somename'] in yii2 assetmanager?
How make access some function in assetmanager?
class AppAsset extends AssetBundle{
public function getLang() {
$currentLang = Yii::$app->session['lang'];
if ($currentLang == 'fa' || $currentLang == 'ar') {
return 'RTL';
} else {
return 'LTR';
}
}
public $lang;
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/iconSprite.min.css',
// how call getLang here
]
How call getLang in css part?

You can do it like this
.. other functions
public function init() {
$this->setupAssets();
parent::init();
}
protected function setupAssets() {
$lang = $this->getLang();
$this->css[] = "css/myfile.$lang.css";
}

Related

Getting __construct() must be an instance of App\Models\Page, none given message

When trying to access a function in another controller, I am getting a __construct() must be an instance of App\Models\Page, none given error message and I am wondering how to solve this...
The controller where the function is called from:
use App\Http\Controllers\Frontend\PagesController;
...
class ModulesController extends Controller {
public function filter(Request $request) {
$items = $this->loadFilteredItems($request->module_id, $request->filters);
$page = new PagesController();
$header = $page->loadElements($request->id, false);
return View::make(
'base.frontend.' . config('folder') . '.includes.filter',
compact('items', 'header'));
}
}
}
The controller that stores the function:
class PagesController extends Controller {
private $page;
public function __construct(Page $page) {
$this->page = $page;
}
private function loadElements($id) {
return 'something';
}
}
Adapted the Controllers into this:
class ModulesController extends Controller {
public function filter(Request $request) {
$items = $this->loadFilteredItems($request->module_id, $request->filters);
$page = new PagesController();
$header = $page->loadHeaderElements($request->id);
return View::make(
'base.frontend.' . config('folder') . '.includes.filter',
compact('items', 'header'));
}
}
}
class PagesController extends Controller {
/**
Removed these lines
private $page;
public function __construct(Page $page) {
$this->page = $page;
}
**/
public static function loadHeaderElements($id) {
return $this->loadElements($id);
}
private function loadElements($id) {
return 'something';
}
}
And it's working now

Laravel 5 layout

I was wondering what a general layout in Laravel 5 is like and if it is in L4 so I wrote some code. I've been using teepluss/laravel-theme but I want something of my own. If anyone could give me some ideas it would be useful.
Controller.php:
<?php
abstract class Controller extends BaseController
{
use DispatchesCommands, ValidatesRequests;
protected $layout = null;
function __construct()
{
$this->setLayout();
}
protected function setPageContent($content)
{
if (is_null($this->layout)) {
throw new Exception('layout was not set');
}
return view($this->layout, ['content' => view($content)]);
}
public function setLayout()
{
$lay = DB::table('layout')->where('selected', 1)->first();//i select the layout from a database
return $this->layout = $lay->name . ".layouts." . $lay->name; //create a file with the database name
}
protected function setView($vista)
{
$view = DB::table('layout')->where('selected', 1)->first();
return $view->name . '/' . $vista;
}
}
After that I call getIndex() in MainController.php:
public function getIndex($page = null)
{
if ($page == null) {
return $this->setPageContent($this->setView('index'));
} else {
return $this->setPageContent($this->setView($page));
}
}
routes.php:
Route::controller('/{page?}/{param?}', 'MainController');

parent class method returns null in its daughter __construct()

I tried a code which I called a parent method in its daughter __construct and itreturns NULL,
I dont know why? I would be very happy if anyone could explain to me why.
Thanks in advance.
Here is my code
<?php
class me
{
public $arm;
public $leg;
public function __construct()
{
$this->arm = 'beautiful';
$this->leg = 'pretty';
}
public function setLeg($l)
{
$this->leg = $l;
}
public function getLeg()
{
return $this->leg;
}
}
class myBio extends me
{
public $bio;
public function __construc()
{
$this->bio = $this->setLeg();
}
public function newLeg()
{
var_dump($this->bio);
}
public function tryLeg()
{
$this->leg = $this->getLeg();
print $this->leg;
}
}
$mB = new myBio();
$mB->newLeg();
$mB->tryLeg();
?>
When I call:
$mB = new myBio();
$mB->newLeg();
, it returns
NULL,
BUT
$mB->tryLeg();
returns e string, 'pretty'.
You have a typo on this line:
$this->bio = $this->setLeg();
You're calling your setter, not your getter, and since the setter doesn't return a value you're getting null instead.
You've also misspelled construct:
public function __construc()
And you need to call the parent constructor.
<?php
class me
{
public $arm;
public $leg;
public function __construct()
{
$this->arm = 'beautiful';
$this->leg = 'pretty';
}
public function setLeg($l)
{
$this->leg = $l;
}
public function getLeg()
{
return $this->leg;
}
}
class myBio extends me
{
public $bio;
public function __construct()
{
parent::__construct();
$this->bio = $this->getLeg();
}
public function newLeg()
{
var_dump($this->bio);
}
public function tryLeg()
{
$this->leg = $this->getLeg();
print $this->leg;
}
}
$mB = new myBio();
$mB->newLeg();
$mB->tryLeg();

php dynamic class methods - scope issue

Hello I'm trying to implement a url router in php something familiar to express.js
Here is the code I have so far.
class Router{
private $request;
private $request_params;
private $params_num;
private $paths;
public function __construct(){
$this->paths = array();
$this->request = strtolower($this->hookRequest());
if ($this->request != false){
$this->request_params = $this->hookRequestParams();
} else {
$this->request_params = array('home');
}
}
public function __destruct(){
foreach($this->paths as $key => $value){
if($this->getRequest() == $key){
$value();
}
}
}
public function get($path, $func){
$this->paths[$path] = $func;
}
private function hookRequest(){
return isset($_GET['req']) ? rtrim($_GET['req'], '/') : false;
}
private function hookRequestParams(){
$params = explode('/', $this->request);
$this->params_num = count($params);
return $params;
}
public function getRequest(){
return $this->request;
}
public function getRequestParams(){
return $this->request_params;
}
public function getPage(){
return $this->request_params[0];
}
public function getAction(){
if($this->params_num > 1){
return $this->request_params[1];
}
return false;
}
public function getActionParams(){
if($this->params_num > 2){
return $this->request_params[2];
}
return false;
}
}
This is used like this as you can imagine:
$router = new Router();
$router->get('index', function(){
echo 'index'; //index is being shown to the browser as expectd
echo $this->getPage(); // This does not work apparently
})
My issue is how could I execute $router methods from within the anonymous function?
As shown in this example with $this->getPAge();
Use a closure..
$router->get('index', function() use ($router) {
echo 'index';
echo $router->getPage();
})
If you define your Closure within your class, $this should be work.

PHP Inherited methods not running

I have the following class hierarchy:
interface IReportGenerator {
public function setTitle ($title);
public function setColumns ($columns);
public function setPrintArea ($start, $stop);
public function setPageOrientation ($value);
public function createReport ($data);
public function saveReport ($name, $path = null);
public function saveReportAs ($name, $path = null, $type = null);
}
abstract class AbstractReportGenerator implements IReportGenerator {
public function __construct() {
$this->report = new PHPExcel();
}
public function setTitle ($title, $now = false) {
// store
return $this;
}
public function setColumns ($columns) {
// store
return $this;
}
public function setPaperSize ($value, $now = false) {
// store
return $this;
}
public function setPageOrientation ($value, $now = false) {
// store
return $this;
}
public function setPrintArea ($start, $stop, $now = false) {
// store
return $this;
}
public function saveReport ($name, $path = NULL) {
return $this->saveReportAs($name,$path,self::EXCEL_2007);
}
public function saveReportAs ($name, $path = NULL, $type = NULL) {
// save report
return $this;
}
public function createReport ($data) {
$this->doHeader();
$this->doTitle();
$this->doColumnHeaders();
$this->doData($data);
$this->doFooter();
$this->doFormatting();
$this->doMargins();
$this->doPrintOptions();
}
abstract protected function doHeader();
abstract protected function doTitle();
abstract protected function doColumnHeaders();
abstract protected function doData($data);
abstract protected function doFooter();
abstract protected function doFormatting();
abstract protected function doMargins();
abstract protected function doPrintOptions();
}
class ReportGeneratorSimple extends AbstractReportGenerator {
public function __construct() {
parent::__construct();
}
protected function doHeader () {
// do nothing
}
protected function doTitle () {
echo 'title A';
}
protected function doColumnHeaders () {
echo 'column A';
}
protected function doData ($data) {
echo 'data A';
}
protected function doFooter () {
// do nothing
}
protected function doFormatting () {
echo 'format A';
}
protected function doMargins () {
// do nothing
}
protected function doPrintOptions () {
// do nothing
}
}
class ReportGeneratorFormatted extends ReportGeneratorSimple {
public function __construct() {
parent::__construct();
}
protected function doHeader () {
parent::doHeader();
}
protected function doFooter () {
parent::doFooter();
}
protected function doFormatting () {
parent::doFormatting();
echo 'format B';
}
}
When I do:
$report = new ReportGeneratorFormatted();
$report->setTitle('title');
$report->setColumns($columns);
$report->createReport($data);
$report->saveReport('file.xlsx');
The output i get is (in no particular order):
'data A'
'format A'
Any idea why 'title A', 'columns A', and 'format B' are not printing? The saveReport() and saveReportAs() methods are working as well.
#zerkins - You are correct, the code I supplied runs as expected. The results I supplied came from the production code, not the sample.
Given that the sample worked correctly, it was as simple as tracking down the difference in the production code. It turned out to be a missing call to "parent" in one of the child classes.
Thanks!

Categories