I am working in CakePHP for the 1st time. I need to create multiple views for a single controller.
Eg: I have a settings table.
Schema of settings table
1.ID
2.Name
3.Type
I have created its model and controller using cake bake. But i have multiple views from where the data goes into the settings table. My data of designations, departments, qualifications, projects and many other things go into the type field of the settings table with their names as entered.
So when i m creating the model and controller thru cake bake it is creating view as per the settings table, whereas i need view pages as per types, i.e Create Designation, Create Departments, Create Projects and also view, edit and delete files for them.
Pls help me find a way to achieve that..
I think you are looking for
$this->render('viewfilename');
create as many views as you want and based on your requirement send then in specific view from controller.
For example:
public function add($type) {
if ($this->request->is('post')) {
...
}
$this->set(............);
switch ($type) {
case 'designations':
$this->render('add_designations');
break;
case 'departments':
$this->render('add_departments');
break;
case 'qualifications':
$this->render('add_qualifications');
break;
}
}
and make view files as add_designations.ctp, add_departments.ctp, add_qualifications.ctp etc in view folder.
You can add Views by creating a .ctp file in the respective Views Folder (Views/"Modelname"/add_department.ctp)
In your "Modelname" Controller you just add
function addDepartment() {
// Logic here
}
But if you just want to set the type, you can create a normal add.ctp and create a Selectbox with all the different possible Types.
You need to read again how the pattern Model View Controller (MVC) works.
If you want to create a new department, you probably want to use the departmentsController associated with the Department model.
In each controller you will have the actions associated with it. This way Cake Bake will generate the add, delete and edit code for each of your controllers.
Of course, you can create them by your own in the controller you prefer making use of the model you wish. But don't expect Cake bake to work differently :)
Related
Yii2 has a feature of generating CRUD for table with gii.
It creates several files: controller, model, search model and views.
What if after generating CRUD I need to add more fields to the table? I see that I need to change:
Model's
phpdoc
rules()
attributeLabels()
Search model's
rules()
search() (tweak ->andFilterWhere() calls)
Views:
index (grid columns)
_form (model inputs)
_search (search inputs)
view (attribute rows)
A lot of work. Is there a way to do it easier/automatically? I understand that I can just regenerate CRUD with gii, but in this case all my other tweaks would be overwritten (lost).
Maybe there exist some other CRUD solution for Yii2, that does not hardcode all fields and allows table schema changes to appear automatically in the views/models? Would be glad to know about it.
If you dont want to overwrite file or add changes manually , you have below option:
In Gii, after clicking preview button you can see list of files.
If file has changes or already in directory than diff button displayed.
On clicking diff button you can see changes in file.
From this model (window) you can modify file as your need or you can copy/paste code in/from temporary file and manage.
Simple way is regenerate model and crud after adding fields to table.
If you have modification in crud, you can generate in GUI files without saving and by copy paste move new column code.
I'm using Laravel 5.1.
I have many views which somehow included in each other. For example, I have the main layout, some modal windows, some custom pages. So basically I have N views.
Let me show you a concrete example. The layout.blade.php is basic layout. The companies/index.blade.php is company list. The modals/companies/create.blade.php is a modal template which is included in layout.
Imagine I have a list of company industries. I need to provide the list to the modals/companies/create.blade.php and to the companies/index.blade.php. The list shouldn't be passed from the controller, because it's kind of code duplication and the list is not related to any conditions, so I don't want to pollute controllers with the same lines of code.
So I have a view composer service provider where I link view composers to views. I may have two approaches:
Each view composer represents small part of data, like "IndustriesViewComposer" which serves industry list
Each view composer represents all data necessary for some view, like "CreateCompanyModalViewComposer"
When I tried the first way my service provide file looked like:
'App\ViewComposers\IndustriesViewComposer' => [
'modals.companies.create',
'modals.companies.edit'
... some other views ...
],
In this case if some of the views are included in the same view the query which retrieves industry list will be duplicated. So if I have many small view composers and corresponding views, then the queries are duplicated in case I include those views together. Since I have many modals which are included together I get the duplication.
In the second way I have exactly one view composer for one view. Same situation happens here. For example, I know that I need to provide industry list for company creation modal, and the same list I need to provide to company edition modal.
View sharing for composers won't work since I need to provide the data only for specific pages for users who is logged in.
So basically the question is: how to execute one query for multiple views in case the views are rendering in the current request and avoid duplication.
Imagine I have a single company page.
1) In every page of my system I have company creation modal which needs industries list
2) In single company page I include company edition modal which needs industries list
3) Single page also need industries list
How to retrieve the list only once and provide it to single view, new company modal and edit company modal views?
You can use a middleware. In the middleware you make a query and save the model (or collection) with a bind to IoC Container.
So, in your middleware you can do (for example):
$my_data = Data::where('some_field','some_value')->first();
App::instance('my_data_saved', $my_data);
do not forget to include the use App;
After that you can access the data where you want (controllers, views, etc.) with IoC Container, for example:
<title>{{ App::make('my_data_saved')->titleĀ }}</title>
Then you only need include the middleware in your routes or controllers where need the data for your views.
I'm trying to figure out the best way to build an application with multiple user types (i.e. Admin, Vendor and Customer).
I'm using Symfony2 to develop the application. I have two options in mind:
Have one controller and check for the user role and render the corresponding view for that role. (One controller, Multiple views)
Have one view and check for the user role to render the corresponding piece. (One controller, one view)
The difference between the above two methods, that is think the first one will have views to be cleaner, without too much logic in it. But will lead to too much logic inside the controller.
If there any better way to think this out ?
The way I do it is similar to your option 2 I am not sure how it works with symphony since I have my own custom framework but its similar in a way:
Controller handles the logic and passes in the parameter to view about user, whether user is admin or has a specific permission
View then renders specific parts of the code depending on this permission/role
Example Controller:
$view = new View('PATH_TO_VIEW');
$view->isAdmin = true;
$view->canDropDatabase = true;
Example View:
<div>
<?php if ($this->isAdmin) : ?>
*** Show some Admin Stuff ***
<?php else : ?>
*** Show Non-Admin Stuff ***
<?php endif; ?>
<?php if ($this->canDropDatabase) : ?>
*** Well I hope you dont do this ***
<?php endif; ?>
</div>
The reason I like this approach is because if I need to update something I only deal with one file versus each view file for each role. Plus for the future if you add roles you need to add a new file while you can just add an extra elsif condition.
I developed a site with Symfony2 and user roles. I didn't change entire pages depending on the role, but there were significant chunks that would change based on role. I went with option 2 and used the logic within the views, and that seemed to work well for me. Cheers
Personally I would go with option 1.
You can minimize the logic in controller by just passing the role of the user as a string/array to your twig template and use something like:
{% include role ~ 'header.html' %}
or in case of an array on which you need to do some checks/apply logic you can use a custom twig extension instead of logic in controllers
{% include roles|check_roles ~ 'header.html' %}
Alternatively you can create a service class and inject it in twig so you can use it's methods inside the view so that you keep your controllers clean and the concern of selecting view/partial for each role to be in one place only.
In Yii crud, I've setup a Model, View, and Controller based on my db table called Form. I've modified the controller and views to my liking thus far:
index.php/form/all (index)
index.php/form/new (create)
index.php/form/2 (view)
index.php/form/2/edit (edit)
index.php/form/2/delete (delete)
Now I'd like to setup some subpages that will be dynamic. The url patterns are below. How do I set this up inside of the FormController.php?
index.php/form/2/fields/all
index.php/form/2/fields/new
index.php/form/2/fields/1/edit
index.php/form/2/fields/1/delete
BTW - Fields is a separate db table with a separate yii model. Though I'd like to not create a controller for it if I don't need to.
Thanks Neophile, I ended up adding a fields controller. Here is a link to my finished solution: Yii URLManager rule for multiple controller action parameters
I'm creating a component in Joomla! 3.1.4 which contains multiple models. What I would like to accomplish is to add multiple models B that associate with model A in A's edit view.
To be specific, I want to keep adding unlimited number of model B to connect to model A when I am editing model A. And provide a simple list of B's in A's edit view.
This can of course be done in B's edit views, or fully hard coded within A's edit view. I believe Joomla! should have some methods to accomplish that to avoid code duplications.
Can anyone please shed a light on this? Thanks!
The Joomla Doc's website covers this subject in the article "Using multiple models in an MVC component"
To call another model from the view, in the view display() method
$modelA = $this->getModel('ModelA');
$modelB = $this->getModel('ModelB');