CakePHP - generating different pages/views dynamically using a menu - php

I'm a total beginner to CakePHP, and I'm trying to build a pretty basic website here that has a menu + some UI stuff like a footer that will persist through all the pages.
The trouble that I'm having right now is that in my controller, I have several different functions corresponding to the website's menu options that grabs data from database, but obviously I wouldn't want to create the same number of views since every page has the exact same setup.
So my question is what's the standard or best practice to achieve this? I read up on elements and am still kinda confused as to how this would be done; how would I keep track of which page is the user currently browsing? And if they click on a menu option, how would it be coded so it takes them from "blah.com/home" to "blah.com/contact"?
I know my question is kinda long and noobish but I'd really appreciate it if I could get some help in beginning CakePHP.

For functions corresponding to the website's menu options that grabs data from database, put these in beforefilter() of the App Controller and use $this->set to set variables and make element for menu which you can call in your layout. You can set the layout in your controllers like var $layout=''.

Related

Codeigniter doing translations from the database

I have been looking around to see if I can learn how to do translations that come from a database.
My aim is to have an admin do the translations for each label in an admin console and when a user selects a language, the site will reload and it will load the translated options from the db instead of having multiple view files in different languages.
So far what I have is adding an extra segment in my url to accommodate different languages ex. www.testwebsite.com/Application/en-US/content
I plan on reading that segment, storing it into a cookie or session and when a new page loads, it retrieves that option from the cookie or session and always loads the right language.
As for the db structure, so far I have
id | label | language | translation
The main issue now is how do I load this properly? My experience so far has taught me that when I want to load a bunch of items from the database, it will come in an array form and in order to access it, I must add a for loop/ foreach loop.
Is there a way to replace the labels without them being in a loop? Or is there a better way to do what I am trying to achieve?

multiple controllers for one view php mvc design

I am very new to MVC.
I just finished reading a book and am trying to implement what I have learned, but I am stuck. In the book and some other explanations I have read online, it's always one controller for one view, like navigation view being controlled by its controller, login form controlled by its own controller.
But I have a header with couple of navigation links, and a search form. Do I separate navigation from searching or assume searching is part of navigation and just control them all in one controller?
First of all, you seem to have the impression that "template" and "view" is the same thing. It's wrong. A properly done view will juggle multiple templates and choose which combination to use based on the current state of the model layer.
As for your navigation & search thing ... well ... it's confusing. Each link in navigation would point to either a different controller entirely or a different methods of controller. And search query would definitely be submitted to a separate controller/view pair.
The navigation+search is just a template, that is used in multiple views as part of the complete response.

Where to place common code in Phalcon-based site that is called each page load

I have code that I want to run on every page load, such as looking up menu items, looking up the users details etc. These will be displayed on partial views that make up the main view.
Where do I place this code so that it can fill my partial views with each page load? I know I can just add the code to the top of the partial view itself, but this doesn't really follow the MVC pattern.
Is there a function that is always called that I can hook into in my base controller?
You can create a base viewmodel for the repeated code and make other viewmodels inherit from it.
...such as looking up menu items, looking up the users details etc
You're a bit unclear about the type of information you want to load: in case the info is a view-component then indeed you should create a base-view and inherit from it or include it (composition) in any other view.
But, in case it is "user-information" - the data should live in a model-component that again, may live as "base-model" object that is included in other model components.

Dynamically Add Multiple Cgridview on a single Page

I'm trying to figure out how to let the user add any numbers of tables or CGridview on a single page. So right now the page has two gridviews in it which will stay fixed. Now i want to add an add button somwhere, clicking on which will give the user another gridview, and so on.
How should i proceed with this. I mean is there an easy way to do this, without resorting to writing everything from scratch.
What i was thinking was to create a new view file using file_put_contents() or fwrite() dynamically everytime the user wants another table on the page? Now following in my line of thinking from where would i create these dynamically view files.
Should i write the whole code of the view and put it in a string, in the controller, and call file_put_contents() from there.
This would cause another problem as the filter needs a specific ajaxUpdate url like this
'ajaxUrl' => Yii::app()->createUrl('project/AjaxUpdate'),
.
Which would entail i would have also have to dynamically create the actionCode in the project controller for the filter in that dynamic grid to work. eg. project/AjaxUpdateDynamic1, project/AjaxUpdateDynamic2, etc.
So i'm kinda stuck with this problem. I would really appreciate if someone points me in the right direction.
Thanks, in advance,
Maxx
if you had an action for ajax loading your gridviews, you can then set your ajax url to that url and you'd have filtering possible for your model, you can even put multiple gridviews for multiple data providers that can be loaded via a parameter that you have sent along with click of your button and an input.

When using MVC in PHP,Should 1 Controller be able to load a entire dynamic user profile?

Im almost scared to ask questions around here these days because I useually get flammed on and told my question is dumb, but here it goes anyways...
Im new to MVCs and im starting to get the hang of them, but one thing im unsure about is if 1 controller is suppose to load a entire profile that has multiple components (i.e a notification beacon, a friend feed, a list of friends that displays thumbnails , a place to post statuses ect...)for the purposes of this post assume im asking if alll the loading for a entire facebook profile should be done in one controller?
Or do i separate each dynamic component into its own MVC and then glue them all together in one main controller or....?
another idea i had was that maybe u do separate each component , but instead of having one main controller you call them as needed in the index.
You could use one controller for the Profile (using your example) and have multiple models fetch and process all the data you need to show. This way even a big page can result in an easy to understand controller that is not so big.

Categories