I'm using codeigniter. I'm currently working on my routes and controller.
Last week, I explored symfony2 and I liked something:
class DefaultController extends Controller
{
public function indexAction()
{
return $this->render('LVIndexBundle:Default:index.html.twig');
}
public function servicesAction()
{
return $this->render('LVIndexBundle:Default:services.html.twig');
}
public function shoppingAction()
{
return $this->render('LVIndexBundle:Default:services.html.twig');
}
In the controller, each action renders a view.
I would like to do the same in codeigniter -> get several functions / actions leading to distinct views.
I'm new to codeigniter. So far, I understood that 1 controller = 1 view.
I'd like to get 1 controller = several functions for several pages. Otherwise, that would be a lot of pages.
Thanks very much for your help!
Based on my experience(s):
In CI, a controller can has more than 1 view php file
ex (function indexAction as a controller function):
public function indexAction()
{
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
}
In Symfony and Codeigniter the controllers are just classes that can hold multiple methods. The methods that are called (called Actions in Symfony) are the place that decides which view(s) must be rendered. One of the main differences between Symfony and CI controllers are the routings that Symfony uses. The routes makes Symfony more flexible then CI.
take a look for an explanation of CI controllers
p.s. Symfony is much more advanced then CI. I like to advise you Symfony :-)
Its absolutely not true that there should be one view per controller. even something as simple as validating a form - you will call different views depending on if the validation passes or not.
Strongly suggest you step through the Tutorial in the codeigniter manual. this is going to answer a lot of questions and its very practical.
Related
So i am into the challenge of in passing the asset for a number of different views.
As far as I am concerned, there are some ways, apart from adding
use app\assets\SomeAsset;
SomeAsset::register($this);
to the layout page or calling out this method.
Although, I am interested, are there any more flexible ways of passing an asset or a group of assets to the whole crud model at once, for example.
Wiggling around me and my colleague found out that this way works as I wanted to:
use app\assets\SomeAsset;
//...Some other dependencies
class DefaultController
{
// add this at the beginning of class in the controller
public function beforeAction($action)
{
SomeAsset::register($this->view);
return parent::beforeAction($action);
}
}
So this might help if you want to pass asset to all files that are under DefaultController control.
Hope this will be useful to somebody.
I'm considering creating a plugin for the admin portion of my application and am seeking some guidance on how it should be structured. I will be using CakePHP 2.5.x.
Part 1:
I will be Auth as well as Acl. Should I be configuring this within /app/Controller/AppController.php or /app/Plugin/MyPlugin/Controller/MyPluginAppController.php?
Part 2:
Let's say I want to access the Post Model: /app/Model/AppController.php from within my plugin. What's the best way to do this within my plugin?
Which one of these files should I create to handle this? Do I even need to create a model within my plugin if I'm just extending the model from the main app?:
//Model
/app/Plugin/MyPlugin/Model/Post.php //Will this conflict with /app/Controller/PostsController.php?
/app/Plugin/MyPlugin/Model/MyPluginPost.php
//Controller
/app/Plugin/MyPlugin/Controller/PostsController.php //Will this conflict with /app/Controller/PostsController.php?
/app/Plugin/MyPlugin/Controller/MyPluginPostsController.php
How should I handle the data within the plugin's Post Controller? Am I on the right track with this?
<?php
// app/Plugin/MyPlugin/Controller/MyPluginAppController.php
App::uses('Posts.Post', 'Controller');
class MyPluginPostsController extends MyPluginAppController {
public function index() {
$this->Post->recursive = 0;
$this->set('posts', $this->Paginator->paginate());
return $this->Crud->execute();
}
}
I realize I've asked a lot of questions, I just want to make sure what I'm doing is feasible/logical.
I have many models in my project. If I'm following phalcon default convention, I will have to create separate controllers for each models. So if I have 30 models and I want to build simple crud page, then I will have to create 30 controllers as well. I'm using the same crud views for each controller
I route the crud to : /project/admin/:controller/:action. The application is well and running but the problems are :
I have to manage every single controllers
Most of these controllers share same methods and attributes, just different table and few validation
So I want to combine them into a single controller AdminController. Here, I listed all the crud-related actions for each models. For example, accountListAction, cityEditAction, etc. Well yes indeed that the page will become much longer (and IDE will took more time to check) but the result is, shared methods and attributes are reusable between each actions.
Now there are only one controllers. I can access 'list' of model 'account' through /project/admin/accountList. To make the routing looks better, I add more route to specific actions so now, I can access it through project/admin/account/list. (had to define route for every single actions)
The question is : Am I doing it wrong ? Is there any better method to do this ?
What I wanted to achieve is : less controller files, less redundancy
if you have some methods, what you need in many controllers, you can make BaseController
class ControllerBase extends Phalcon\Mvc\Controller
{
public function something_what_you_need_in_many_controller(){
}
}
and then use it while creating controllers:
class PeopleController extends ControllerBase
{
}
so you will have that function something_what_you_need_in_many_controller() in PeopleController.
You don't have to create controller for every model. These 2 are different layers of application, you have controllers - they tells in what view put what data and it can use many models at once. Models are just to handle the connection of data between controller and database.
I think your approach is good but you just need to organize it a bit diferent to make your life easier. Maybe something like this:
As you already did, combine your CRUD operations in a single
controller (AdminController)
Instead of creating a action for every combination of CRUD/Model (like cityEditAction), create one action for each CRUD and any aditional action you want (exportToCSVAction for example)
Then at every action you receive as first parameter the model type like list/city and if needed the id as second paremeter like list/city/2.
After this every action can be implemented to work with any model type. Here's a example of a deleteAction():
public function deleteAction($modelType, $id)
{
if($this->modelTypeExists($modelType))
{
//Check if the user has the required privilegies for this action
//See http://docs.phalconphp.com/en/latest/reference/acl.html
if($this->acl->isAllowed($currentUser->accessGroup, $modelType, "delete"))
{
//Get the model being manipulated
$model = $this->getModelById($modelType, $id);
//Attempt to issue the required action
if ($model->delete() == false)
{
//Something went wrong with the deletation
}
else
{
//Horray!!
}
}
else
{
//Denied
}
}
else
{
//Error
}
}
NOTE: modelTypeExists and getModelById aren't built-in functions, they're just illustrative functions for the answer.
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.
I am trying to learn how to use the Zend Framework and ive ran into trouble. Im trying to place the current users name in the header of the application (displayed on every page), specifically /layouts/scripts/default.phtml.
The MVC architecture is very new to me and confusing me greatly. I do not want to have to place the logic to display this username in the controller every time (this is probably the wrong way to do it anyway), so where would I place the code to assign this variable if not in each controller?
Cheers
This is the kind of thing that action helpers were designed for. A full tutorial on them is a bit beyond the scope of SO, but there are several good tutorials available.
Start with the Zend Framework Documentation and then take a look at Mathew Weier O'Phinney's tutorial and also this one by Rob Allen.
The issue with using a base controller for this kind of thing is that the resources are loaded regardless of wether your controller needs them or not, whereas action helpers are loaded only if needed.
I almost forgot the excellent ZendCasts have a video on action helpers.
You want a base controller and to assign that in the preDispatch method:
class MyApp_Controller_Action extends Zend_Controller_Action {
public function preDispatch() {
parent::preDispatch();
Zend_Layout::getMvcInstance()->assign('username', getCurrentUserName());
}
}
Then extend your own controllers with that new class:
class MyApp_Module_ActionController extends MyApp_Controller_Action {
}
Then in your layout view:
echo $this->layout()->username;
First, read the manual, and than try to accomplish something like this:
class BaseController extends Zend_Controller_Action {
public function preDispatch() {
// your logic to show the user name goes here
}
}
class SomePageController extends BaseController {}
class SomeOtherPageController extends BaseController {}
This will most likely solve your problem.