Overriding route in Cakephp - php

I have one trouble in routing in CakePHP. I have index action of all customers. And the question is, is there a way to make it this way, when I go to /..../.../customers CakePhp renders index(as per default), but when I am going to /..../.../customers.json(in .json format), CakePhp renders another action, where some array is serialized. I already enabled mapping resources, so it works just fine without overriding, but is there some way I can implement this ?
I've already read https://book.cakephp.org/2.0/en/development/rest.html.

Thank you, Gransfall. I just check if the request is json and then load the view in the need way.
if(isset($this->request->params['ext'])){
if($this->request->params['ext']=='json'){
//here setting serialized array
}
}

Related

Where should I store the Laravel Session data based on the MVC pattern?

I'm working with the Laravel integrated to the WordPress and struggling to understand where should I put the session data based on the MVC design pattern?
Back in the day, I used to put everything inside the view (header.php and footer.php) files and after some time, it became a mess, complete mess.
As written here:
As MVC I use CodeIgniter, so I don't know if this can be true for your specific environment, but I usually set session values from the controller. It is possible to do it even in view but the correct way is to keep code in controller (as keeping database stuff in models).
In the controller, you can use standard php $_SESSION array or, it it exists, your framework session class.
Yea, I understand it's a good practice to not mess around with the view and put session variables inside the controller. Here is the problem:
As I'm using the WordPress, the goal is to have a place where the session variables are always loaded, doesn't matter if I changed the theme or anything, they should stay in the Laravel backend.
Without any testing, I could think about a couple option:
Use Laravel Service Provider and insert session variables inside the boot function.
Use Laravel Middleware functionality, however, not sure how to implement this.
You can use the laravel https://laravel.com/docs/5.6/session Session helper.
Then you can just do Session::put('hello','world'); Session::save(); and retrieve it with Session::get('hello'); You can do this anywhere you'd like, as long as you remember to save the session after putting things in it, modifying things or removing things.
As long as Laravel is loaded and the domain has the laravel session cookie, you can access them.

Setting and getting session variables in Bolt CMS

I have a web app running on Bolt CMS and I need to be able to save some information across page loads so it's persistent. The data needs to be set via an AJAX call and retrieved within a Twig template. The trouble is, I don't know how I can do this within the Bolt environment (I've never used Symfony before)
I've seen quite a few similar questions on how to retrieve session variables within a Symfony controller but nothing on how to edit (or add a new) controller so that I can call it via AJAX to set the session variable
Thanks
Because twig is rendered server-side, I assume you want to set something in one request, and then fetch it again on the next. I think you will need to create an extension for this, that stores the data in a table, and allows you to fetch it later. Take a look at the "WaffleOrders" extension for a good example on how to do this: https://github.com/bolt/WaffleOrders
This is all happening on the bolt side, though. To make it ajaxy, you should use jquery's ajax functionality to POST or GET the data, as needed.

What is the right way to pass parameters to a Kohana 3.3 controller called through HMVC?

I am trying to use Kohana 3.3 HMVC approach. I have created a view and a controller for generating a segment of a page (meant to be integrated into the actual web page by another controller and to be never accessed through HTTP from outside a controller) filled with data records retrieved through ORM. What I need is to pass some data (records selection criteria) from the top controller through the middle controller to the ORM model. Altering GET/POST data as suggested here seems wacky (as it is going to alter the whole application state rather than of the target controller as far as I understand) (but perhaps it is considered okay in the PHP world, I don't know (I am coming from the strict C++/C#/Java/Scala world), let me know if so). Is there a better way?
The HMVC approach works just like a normal request except that it has its own instance of the request class. From the HMVC sub request you can access the parent request object by loading the initial request.
$parent_request_query = $this->request->initial()->query();
You can also access the current request.
$current_request_query = $this->request->current()->query();
You could also just pass parameters.
$params = array(
'passing' => 'data'
);
echo Request::factory('controller/action')->query($params)->execute()->body()

How to implement sending JSON in CakePHP

I have to create web page for testing and I don't know how to implement it's logic (because I don't want to break MVC). It will be created in CakePHP.
Base thing I want to do is, that presenters action will have a parameter (JSON object or JSON string) and based on this parameter, there will be created a testing form inside view.
After user submits the answer, it will be sent to my PHP algorithm as a parameter(type of JSON object or string). This algorithm will return another JSON object that will be used as parameter for above mentioned presenters action.
I don't know how to implement this logic of sending and receiving JSON in order of not breaking MVC rules. Please explain it to me.
The first thing to do is think about your design.
The controller can process your data and return a JSON response, so you'll want to use that. Luckily Cake has this built in. Have a read of the book, http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
If you are passing JSON into your controller, you'll want to just pick it up out of the request. You'll be able to find it by debugging the request inside your controller. debug($this->request).
Then you can process the JSON in your controller, do some stuff and return a JSON response.

MVC or CodeIgniter limitation: how I can access a 'sub' controller?

I don't know if I'm wrong. But all I know is that in MVC, is that the Controllers is always responsible for calculating the data, and the View to print them.
The problem
I have a poll that I need use a model to get your data and I use a view to print. Currently I can access this poll by use the url "poll/last". This works fine.
My problem is that I need print this info on some pages (like in "site" controller).
The Dilemma
If I simple load the poll view on page, nothing data is get from model. Onetime that it is work of Controller.
If I move all controller part to view, this works fine. The low point is that turn the application in a "non-MVC compatible".
The Solution
So how can I solve this dilemma?
Actually the CodeIgniter not is a HMVC, and the HMVC module don't works fine -- only locally.
There are some viable solution to solve this problem?
Controllers are not always responsible of all calculations. When those calculations are part o fthe 'business or data model' they should go into the model. My english is not good sometimes but i'll try to explain with an example: Let's say we have a table with persons data, and a column birth_date. Age() function should be in Model, because is another way of seen birth_date.
In your case, I would try to move calc into the model, and write a partial who shows the result, and pass the resulting partial view to the main one. Something like
$data['poll_view'] = $this->load->view('poll_partial',$this->poll_model->getPollData(),true);
$this->load->view('current_view', $data ); //that includes poll subview
I may be misunderstanding your question, forgive me if I'm wrong. I can't comment yet on posts, so consider that this may be an answer.
You're wanting to use the data that is in the poll/last controller in another one?
Why not have the same code that you're doing on the controller, in a method/function in the model?
That way, when you call your model just like in the poll controller, the same code and data is available to the site controller.
That's the main function of models.
In my opinion it goes like this:
View - Displays the data, formats the data, and puts the data where you want it.
Controller - Takes the data and determines what exactly to show and on what URL / location to show it, and in which view.
Model - Gets the data from the database, does any calculations that you need to, and returns it.
Hope this helps!
It sounds like you are trying to load the poll view into other views? If so, take a look here: http://codeigniter.com/forums/viewthread/189935/
The same question has been asked/answered
Forgive me if that wasn't your question... can't comment on posts...

Categories