Alter auto render for controller - php

Is there a way to alter the autorender views in cake 2.3.5, either but overriding something or method name conventions?
I want to render views in subfolders of the main Controller directory, here is an example:
class AdministrationController extends AppController {
public function products(){
$this->render('/Administration/products/index');
}
}
I would rather store the views in organized subfolders like this:
- Administration
- products
- index.ctp
- edit.ctp
My question is: Is there a way to rework this so that I don't have to use $this->render() in every action?

Yes
Either call render with a path relative to the corresponding view folder:
$this->render('products/index')
Or call with an "absolute" path which is understood to be relative to the view folder:
$this->render('/Administration/products/index');
Both of these calls will render the view file app/View/Administration/products/index. If you want to structure your view files like that - you either call render in each action or manipulate the viewPath variable to point where it needs to be (either in the class, or in the beforeFilter).
An Administration controller is not normal
In the question there is Administration , products and index - all normal things, but it's not normal to have an Administration controller. With that kind of controller structure the Administration controller will become huge.
The normal way to do that would be to use admin routing and define an admin index:
class ProductsController extends AppController {
function admin_index() {
//
}
}

Related

Using custom named controllers with codeigniter

Hi I'm working on a application that uses a admin environment and a user environment. To make my code and controllers more readable I would like to use some kind of a prefix for my admin and user controllers. Or maybe use a different directory for the admin en user controllers. Without using the exact name in the url to call the controllers.
Does anybody know if there is a way to do this or a work around for my idea.
For example I would like to use: user.[controllername].php or user_[controllername].php
EDIT:
So when I use routes to separate the user controllers from the admin controllers, it brakes my template resolver.
For instance lets say the url is: http://myapplication.com/user/profile in normal cases this would call the user controller and the function profile. My template resolver looks for the folder user and checks if there is a level1.tpl file.
Lets say we want to edit this profile the url would be: http://myapplication.com/user/profile/edit/1 now it would search in the folder user for a level2.tpl file and would call the function edit with id 1.
The problem with the routes is that it prepends something to the url witch I don't want because then it searches in the wrong folder for instance when we use admin/dashboard it will look in the folder admin instead of the folder dashboard.
So basically what I only need is only to tell the application to use a different directory where the controllers are located or a different filename base on what kind of user is logged in.
Thanks in advance, for sharing you knowledge.
You can do that in many ways.
1. Create two file in application/core named "Admin_Controller.php" and "User_Controller.php" with following class signature,
class Admin_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
and
class User_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
In these classes you can do use your PHP logic to do whatever you wants, just simply extends your Controllers to Admin_Controller or User_Controller.
eg: class Home extends User_Controller {} or class Dashboard extends Admin_Controller {}
2. You can also simply do that with routes.php
eg:
// for admin
$route['admin/(.+)'] = 'admin_$1';
// for frontend
$route['(.+)'] = 'user_$1';
I hope this might give you some idea.

How to pass data to layout in Laravel regardless of controller?

Let's say I got ControllerA and ControllerB which both implement the same layout. Now I want to pass data to layout, for example, a message that should appear in layout no matter which controller implements it. If I had only 1 controller, I would do something like:
class Controller extends \BaseController {
public function setupLayout() {
View::share('message', 'Hello world!');
}
// further methods
}
However, when I want multiple controllers to implement a layout, I have to do this in every controller, which doesn't sound reasonable. So I wanted to ask, is there any native way in Laravel to pass data to layout and not to copy code in every controller.
Thanks in advance!
For those cases I would recommend a view composer, where you can set them for more than one view (or layout) or just all of them:
View::composer(['store.index', 'products.*'], function($view)
{
$view->with('model', 'one');
$view->with('colour', 'black');
});
You can put that in your routes file, filters file or, like, me, create a app/composers.php and load by adding
require app_path().'/composers.php';
To your app/start/global.php.

How to return view in zend 2 like laravel

I need to make a zend 2 web app but with zero knowledge in it.
In all the tutorials I've seen, they all set the file names of the views in the module.config.php, in laravel all you need to set is the route for the view, and you can set in the controller what file to give to that route.
So how do you turn this laravel code to zend 2 code:
$fileDirectory = isMobile() ? "viewfiledirectory/viewfile_mobile" :
"viewfiledirectory/viewfile";
return View::make($fileDirectory);
Or if it there is no zend 2 equivalent of that, at least how do I make it like that, every file is hard coded in the template map, how would I be able to make the controller point to that route.
This is a big site, at least 8 pages(only one will be used depending on language set and depending on the user's device as well) per route, I can't just list all the files in the template map of the module.config.php, I want it to be like what I do in laravel.
In Zend Framework 2, you implement a view as a template file, which is a file
having .phtml extension ("phtml" stands for PHP+HTML). View templates have such
a name because they usually contain HTML code mixed with PHP code snippets used
for rendering the web pages. Views typically live inside of the view subdirectory
of the module.
In your controller, you use the ViewModel class for passing variables to the view template. The view model can also be used for setting the view template name (see an example below):
<?php
// IndexController.php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController {
// The "index" action
public function indexAction() {
// Pass two variables to view template
$viewModel = new ViewModel(array('var1'=>$var1, 'var2'=>$var2));
// Set view template name
$templateName= $this->isMobile() ? "index/viewfile_mobile" : "index/viewfile";
$viewModel->setTemplate($templateName);
// Return the view model
return $viewModel;
}
}
If you need a basic explanation of views concept in Zend Framework 2, I would recommend you to read the Using Zend Framework 2 book.

Would bypass of the controller in CodeIgniter be considered a good practice?

Is directly call to the Model class inside the View is best practice or not? Currently I am using CodeIgniter to develop an application. In different Views of my application I'm including menus that I want to pull from the database. And the thing is currently I am passing the values to the menu through the controller. If I make a common model class and call it from the View and by pass controller. So that there will be one call to Model and it will load menu from the database at once and by pass the controller. By doing this what pros and cons will come?
With codeigniter, your views should not be concerned as to where data comes from, only that it exists. Only your Controllers should be in direct contact with your Models.
It sounds like you have a common menu that you want to load in your views and you don't want to replicate that code across all your Controllers.
To solve this problem, you need to create a common controller that your primary controllers inherit from with a method that fetches the menu.
My_Controller needs to be saved to the core folder in the application directory.
class MY_Controller extends CI_Controller
{
protected function get_menu()
{
// Load your menu here
$this->load->model('menu_model');
return $this->menu_model->get_menu();
}
}
All your primary controllers will inherit MY_Controller
class Home_Controller extends MY_Controller
{
public function index()
{
$page_data = array('menu' => $this->get_menu());
$this->load->view('home/index', $page_data);
}
}
In my opinion no its not best practice. View should contact Controller and Controllers should retrieve data from Model where Model does all the logic. Controller suppose to be the glue or middleman of View and Model.
Controller passes the returned data into View and then you do your foreach loop or whatever to display it.
View should not be doing any logic. Retrieving data from database is somewhat logic.
It is a worst practise. Then why we have controller concept for MVC. :) Mainly all logics goes into Model so thats not good to call model directly.
There will not be any problem, since you are using a common controller.
Note : You should not call model directly from view.

mvc pattern, frameworks, implementations

Hello i have a few questions about mvc pattern and frameworks in general.
I know mvc stands for model - view -controller and that models are fat and controllers are skinny but i'm not quite sure about few details .. on the view part.
let's say for example i have this model
<?php
class Menu_Model extends Models
{
public function listMenuItems()
{
return $this->query('some_select');
}
}
controller
<?php
class Menu_Controller extends Controllers
{
public function index()
{
$this->load('menu', 'Menu_Model');
$this->view->assign('menuItems', $menu->listMenuItems());
$this->view->add('menu.php');
}
}
view
<div class="menu">
<li>{echo_some_data_from_controller}</li>
</div>
The above code let's say is for a simple menu fast wrote now .. as an example. by the mvc ideea it needs to have a model a view and a controller good but then how do i implement this menu in each of the views i have? let's pretend that:
the head.php file where i keep the import css starting of the html with basic stuff and the header of the website to get data from mysql for the menu i would need to call the model but the model is called in the controller and each page got it's own controller so from what i understand so far for each controller method i would need to call for a certain model menu, login form etc... to output on each page i need to get data or how do i do it ?.
The responsibility of a Controller is to handle User Input. If your menu doesn't require any user input, put the code into a View Helper and then fetch the Model data from there. Then call that View Helper from the View. In other words: you dont need a controller then.
An alternative would be to provide some sort of mechanism that allows you to register common functionality on each call to a Controller, e.g. something like Zend Frameworks's pre- and post-dispatch hooks. You could then write plugins for these hooks to load and inject certain Model data on each request.

Categories