how to use a controller's action in all controllers - php

I have an action that renders search view to do search as the above search bar in this website, so its should be shown in every view.
I don't know what is the mechanism to do it. for example if I make the search action as a widget this will not be fine, because the results of search will be shown in the same position of the search widget ( at the top of website).
so, how I can make a search action that should be shown in every view in the website?

In order to resue same search function in everywhere, you need to create a widget.
I have explained briefly in How a widget works, then you can attach it in every view that you want.
If you don't have any idea to begin, check this out: Yii ESearch
Here are some references that would be useful:
how-to-use-a-widget-as-an-action-provider
actions-code-reuse-with-caction/
Yii Widget

If you want to add something to every view then you should add it to the layout. By the sounds of it you don't need to use a widget at all, although it would probably help with code maintainability.
You never mentioned a requirement for ajax so keep it simple and don't use it. When someone enters a search and clicks submit (or presses return) then the form submits to the SearchController. This way there is no need to have a search action in each controller.
If you particularly want the same action in every controller then create a Controller base class with that function in it and inherit from it to create all your other controllers.

Related

PHP MVC Form Submission

I have a question related to form submission done in PHP application that's built in MVC architecture (self-written framework).
All examples that I've seen so far (including existing back-end frameworks) work this way that once form for adding record to database is submitted then certain method of controller is executed [say i.e. addRecord()], which triggers method of appropriate model. If everything goes OK then record is added and controller's method [addRecord() in this example] renders view of "index" page that displays table with records from database.
What I would like to achieve is to render view with form used to add records (the same that I used to add first record) instead of "index". Obviously I can do it easily by just rendering appropriate view from addRecord() (view with the form).
But the tricky point is when you check url you'll see the following:
The first time you enter it will be i.e.
http://project_name/my_controller/create
Once first form was submietted and you return to the view from addRecord() method then url will be:
http://project_name/my_controller/addRecord
What I would like to see is return to the original url, that is http://project_name/my_controller/create
Not sure if this is clear?
PS. Of course I could use AJAX call for form submission (that way I will stay at the same page) but perhaps it's possible to achieve the same without AJAX.
Thanks in advance,
On the controller you will want to submit to the addRecord route and do the processing. Have a check to make sure it was successful and on successful submission you can redirect back to the create route.
It is hard to give an example since you are using a custom made framework. I use slim which has a redirect method for a route. If what you have made does not have something like that then using should do the trick.
header('Location: '.$createUrl);
die(); //or exit

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.

How to make a view 'portable' in Codeigniter?

I have in my index a place where I want to include a search form. Thing is this 'search form' needs to be in different sections of the site, so what I did was to create a controller and model for search form view. Now my problem is I don't know how to include it in my index (or in other parts of the site).
I already know it goes against the MVC pattern to load a controller from a view, so I stopped looking for that. I've done some research and the most common answer seems to be to 'reuse the model' but I'm not sure what that means. Should I copy the functions in the controller of the search form and include them in the index controller?
First of all, I think you should take a look at the template method, if you're not already using it.
http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html
This enables you to make custom templates and you can decide in your controller whether you want the search form in it.
If you'd rather do this in the view itself, you could try using this in your view
include(searchform.php)

Joomla: How to load view from custom Jtoolbar button?

I'm trying to create a custom Jtoolbar button in Joomla 2.5. I'd like the button to act very similar to the built in edit button, except I'd like it to be called generate. The button should load a generate view (similar to the singular edit view). It should have form fields for the user to fill out and then click a submit/save button that will run a php module to populate the database with calculated values.
My google searching has been very unproductive. So far I've been able to create the button with in games/view.html.php:
JToolBarHelper::custom('games.generate','extension', 'extension', 'generate', false);
I believe this should call a controller method in controller/games.php called generate().
in controller/games.php:
public function generate()
{
JRequest::setVar('view', 'schedule');
Jcontroller::display();
}
After much muddling around, this appears to be loading the view and a template tmpl/default.php. This seems wrong, but it's the furthest I've gotten so I'm going to keep going with it until I get it all figured out.
Original Question:
How do I get that controller to load the view/form/fields/template and then run the php script to populate the database. I probably just need a point in the right direction to figure this out. There doesn't seem to be any complete tutorials/examples on adding a custom button.
A couple of years old, and it's for 3.2, but I added this to my controller for a custom view. I needed a layout called insert. I don't know why you couldn't do the same to just change the view.
public function insert()
{
$this->setRedirect(JRoute::_('index.php?option=com_mycomponent&view=date&layout=insert', false));
}
Calling it the same way from a custom Jtoolbar button. It works, but maybe there is still a better way.

Categories