how to call action from a view of another action? - php

how I can call an action from the view of another action, knowing that the 2 actions are belong to the same controller ?
the controller is named FilesController, and the requested action is named Subscribing.

Actions shouldn't really be called from within the view. The view should only have data fed into it that will be represented by the view code itself. Views can, however, call other views. You may want to look at how your logic is structured to see if there's a different way to go about doing this.
There is one way, though, and that's via ajax. You can make an ajax call via Javascript/jQuery to a controller, and then have that controller send back a view, which is then placed into a specific area of the original view file.

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

Mvc and php: passing variables from controller to the view

I'm trying to implement mvc design pattern to my existing dating-web-app project. I believe I understood the concepts and created a login model and controller without any problems. Basically, I have a base controller which initiates a view object on construct. Than I use that object to send information to the view from within the controllers as method parameters. However, I couldn't decide how to send variables to the top menu view. Since this menu displays dynamic information (request count, notification count,etc) and included in every page of the application, only two options come to my mind:
Send information to the view from every controller, which seemed like reuse of code.
Put a seperate action in the base controller and make it called automatically when other controllers extend base controller.
Which one should I choose? Or, is there a better way to do this?

How to render a template in a controller?

I have a blade template called 'main' and I wonder how I can render a sub template by calling a controllers method in my main template. Lets say I have a Controller WidgetsController with a method getSubView. The method returns a specific view with some data from (for instance) a database.
I already tried to #include a template but this will not call the controller which sets some necessary data to the view.
Thanks.
views don't call services, they only take variables and put them on screen for presentation.
you're on the good side with #include(). you only need to gather the infos for that sub view beforehand in the controller, and pass it to the View::make('main')->with($vars).
you may also consider using another <?= View::make('subview')->with($vars->sub);?> within the the view. or just use the #extend functionality.
I don't entirely understand your question.
I think what you are looking for is a View Composer
It allows you to get data for a subview without having to create the data in every controller.

How PHP MVC should look like with jQuery/javascript code?

Well, I've read this tutorial if I could say: http://www.symfony-project.org/book/1_1/02-Exploring-Symfony-s-Code
And, actually, I write my code very similiary. But my question, where should I insert my jQuery code? I am sure it should be in the part of the View, but are there any good examples on how should I combine it to make "live" websites?
Edit: By saying live, I mean, for example, send POST request through Ajax and get information and similar. So, no refreshes for people.
Thank you.
jQuery as a part of javascript resources should be included in html.head or in-place, depending on what should jquery do and if the code is reusable for more views.
So it has to be part of View and you're choice is to set it up in layout or action view itself
If you need the javascript on every page then add it to your master view if not then just include it to the particular view files.
In context to codeigniter:
I extend the controller with MY_Controller and initialize a property as array which would hold the scripts that are added dynamically to the view.
eg.
var $templateData['scripts'] = array();
The controllers then pass $this->templateData to the views
And the views load the extra scripts( available as $scripts) as directed by the controllers in the head tag
This way you can even avoid loading the main jquery file when not needed.
Loading jquery.js only for the controller that need it.

kohana setting url for Ajax Modal Form

I have a Kohana controller, and a route corresponding to it.
also, i want to make a form that will appear as a ajax modal, when a user clicks a link. The problem is: i want this form to be controlled by a different controller action, and of course, because i am using exactly the same data as in the view using the first controller action, i will have exactly the same parameters (in the second controller action).
The problem is that, having the same parameters and only different actions, my both controlers's routes will point in the same page. So...
How can i make an ajax modal form that will use exactly the same variables that the view in which i am putting the link to the modal form is using? is it indicated to make another controller action?
Thank you!
I guess you just should create 2 different views.

Categories