Laravel 5 layout - php

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');

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

use custom function in assetmanager

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";
}

My CI model class with static method is failing

OK...first I read this article..
http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/
In the first hack, it says if I include these in my config.php file:
function __autoload($class) {
if (file_exists(APPPATH."models/".strtolower($class).EXT)) {
include_once(APPPATH."models/".strtolower($class).EXT);
}
}
I don't need to extend my model class to CI_Model.
Then the I tried to use $this->db->select('email')->from('users')->where('email', $email); in my model class..CodeIgniter tells me "db" property is not found. Has anyone tried this hack before and know how to get it to work?
class User {
public $id;
public $email;
public $displayName;
public $password;
public static function search_by_email($email) {
$user = new User;
$user->db->select('email')->from('users')->where('email', $email);
$query = $user->db->get();
if ($query->num_rows()==0) {
return false;
}else {
foreach ($query->result() as $key => $val) {
$user->email = $val['email'];
$user->displayName = $val['displayName'];
return $user;
}
}
return null;
}
When I call the static function in my controller I use:
$user = User::search_by_email("xxx#gmail.com");
If I don't extend User, it will show a CI warning: Message: Undefined property: User::$db. If I use User extends CI_Model {}, the server will crash.
Does anyone have an idea about this situation?
Edited:
Then I dumped the silly Nettus' tutorial code, thanks to you guys...but my model is still not working:
class User extends CI_Model {
public $id;
public $email;
public $displayName;
public $password;
public function search_by_email($email) {
$user = new User();
$this->db->select('email')->from('users')->where('email', $email);
$query = $this->db->get();
if ($query->num_rows()==0) {
return false;
}else {
foreach ($query->result() as $key => $val) {
$user->email = $val['email'];
$user->displayName = $val['displayName'];
return $user;
}
}
return null;
}
In my controller I use:
$this->load->model('User');
$user = $this->User->search_by_email("leo.niecn#gmail.com");
My server still crashed.....am I forbidden to write class variables like public $id in my model?
This part of code doesn't make any sense:
function __autoload($class) {
if (file_exists(APPPATH . "models/" . strtolower($class).EXT)) {
include_once(APPPATH . "models/" . strtolower($class).EXT);
}
}
as long as you can load your model from autoload.php under config folder or just do the following when you need the model:
$this->load->model('model_name');
then just call your function.

Action_Helper in separte module does not get loaded

I followed this great article http://weierophinney.net/matthew/archives/246-Using-Action-Helpers-To-Implement-Re-Usable-Widgets.html, but currently i can't get work my simplified example.
PROBLEM The preDispatch does not get loaded.
I created new module user (there is also controller UserController, i hope this wont mess up the loading).
I have added two files in user.
Bootstrap.php - under module user
class User_Bootstrap extends Zend_Application_Module_Bootstrap {
public function initResourceLoader() {
$loader = $this->getResourceLoader();
$loader->addResourceType('helper', 'helpers', 'Helper');
}
protected function _initHelpers() {
Zend_Controller_Action_HelperBroker::addHelper(
new User_Helper_HandleLogin()
);
}
New folder under /user/helpers and class HandleLogin.
class User_Helper_HandleLogin extends Zend_Controller_Action_Helper_Abstract {
protected $view;
public function preDispatch() {
echo 'called';
if (($controller = $this->getActionController()) === null) {
return;
}
$this->createProfileWidget();
}
public function createProfileWidget() {
if (!$view = $this->getView()) {
return;
}
$view->user = '<h2>HELLO WORLD</h2>';
}
public function createLoginForm() {
}
public function getView() {
if ($this->view !== null) {
return $this->view;
}
$controller = $this->getActionController();
$view = $controller->view;
if (!$view instanceof Zend_View_Abstract) {
return;
}
//$view->addScriptPath(dirname(__FILE__) .'/../views/scripts');
$this->view = $view;
return $view;
}
}
And lastly added into layout.phtml the output.
<?php echo $this->user ?>
is init() function of User_Helper_HandleLogin works? is User_Bootstrap works? :) maybe you forget resources.modules[] = in config.ini?

In Zend-Framewok plugin, how to do somthing simular to $this->view->foo =...;?

i wrote a small plugin, so i will be able to get the name of the controller in each view.
but idk how to "pass" a parameter to the view (do sumth like $this->view->foo =...;).
class Zend_Extension_Controller_Plugin_GetControllerName extends Zend_Controller_Plugin_Abstract
{
public function __construct()
{
}
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$this->view->controllerName = $request->getControllerName();
}
}
what can i write instead of $this->view->controllerName so it will work?
Try this:
$view = Zend_Layout::getMvcInstance()->getView();
$view->controllerName = $request->getControllerName();
You can use the helper broker to get an instance of the view. Something like this should work:
Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view->foo = 'bar';
Take this example as basis:
class Plugin_Sidebar extends Zend_Controller_Plugin_Abstract {
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
if($request->getModuleName() == 'admin')
{
return;
}
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view) {
$viewRenderer->initView();
}
$view = $viewRenderer->view;
$Categories = new Model_DbTable_Categories();
$view->menuItens = $Categories->getMenu();
}
}

Categories