We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Page from more actions/views

Hello,

I am interested in Phalcon but totally newbie. Thinking about refactor my apps with Phalcon. I am using pages containing more parts, for example DNS settings admin page:

<h3>A records</h3>
<div>table with A records</div>

<h3>CNAME records</h3>
<div>table with CNAME records</div>
etc.
<h3>Zone settings</h3>
<div>table with zone settings</div>

I want to have possibility to refresh every table (or div) with Ajax. So with DRY is possible to do something as this in controller?

public function indexAction() {
    // use showARecordsAction() 
    // use showCnameRecordsAction()
    // showZoneSettingsAction()
}
public function showARecordsAction() {
}
public function showCnameRecordsAction() {
}
public function showZoneSettingsAction() {
}

Every show-action should use its own .volt file (because of refreshing its content with ajax).

Or what is the best way to do this? Thanks, J.M.



85.5k

i would do it like this:

main.volt


//in the controller you fetch all the data you need and forward them to the view

include A_records.volt

include Cname_records.volt

include zone_settings.volt

after that with those actions you need to get the nesssery data for this view only and echo 1 of the particials.

https://docs.phalcon.io/en/latest/reference/views.html#simple-rendering

//i am not using volt so i dont know the exact syntax

Just fetch all data in one action. One action means one request, you shouldn't call other actions in controller. Well, you can eventually create private methods if you gonna use the same code twice - but better to create service. Check volt documentation how to use include/partial - https://docs.phalcon.io/pl/latest/reference/volt.html#view-integration

Thank you very much Izo and Wojciech for your explanations.

So is for example this best way for my purpose?

# controllers/DnsController.php:

public function indexAction() {
    $this->view->setVar("recordsA", getRecords("A"));
    $this->view->setVar("recordsCname", getRecords("CNAME"));
    //etc
}
public function showCnameAction() {
    $this->view->setVar("recordsCname", getRecords("CNAME"));
    $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
}
---------------------------

# views/dns/index.volt:

<h3>A Records</h3>
<div id="recordsA_refreshable_by_ajax">{{ partial("dns/showa", recordsA) }}</div>
<h3>CNAME Records</h3>
<div id="recordsCname_refreshable_by_ajax">{{ partial("dns/showcname", recordsCname) }}</div>

This is without "Simple Rendering".

Also - is good practise with Phalcon to have getRecords() method in this DnsController.php? Hope yes because need to check zone and permissions etc.

Thanks, J. M.

But what is even getRecords ? You should have method, are even now about something called OOP ? Are you sure you want to return view when doing ajax ?

getRecords() may return array with records for view. View creates table (or something similer) of records or "you have no records" notice for user.

I think to get basic view into <div/> by ajax is more simple for me for cases when record is changed, deleted or added than modification of this table by javascript. Bad practise?

edited Jun '16

Well i only return json bymyself and update view in html, but im using angular so i don't have problem with such a thing. but getRecords() should be method, not some weird function, learn about OOP first - then start use phalcon or any framework.

Thank you. As this DnsController is a class I suppose that function in it is method :) what I asked you if is good practise to have helper METHOD as getRecords() (which is not action-method) in controller class ...

edited Jun '16

Then you should have it in some service or helper, but it shouldn't be just getRecords.

edited Jun '16

Thank you. As this DnsController is a class I suppose that function in it is method :) what I asked you if is good practise to have helper METHOD as getRecords() (which is not action-method) in controller class ...

Is the data coming form a database? from an external service? where is htis data coming from?

If you are using it form a database why are you not using the phalcon ORM? or at the very least the PHQL?

If the data is coming from and externals ervice write a helper class with your $helperClass->getRecords() method on that class.

Your question has multiple parts. It totally depends on how you are handling the ajax refresh... are you building the table from raw data? are you rebuilding the talbes from raw json? or are you wanthing to do something a little more cut and past.

    $('#thing').empty().html(newHtml) 

I think if you are going to take the time write the ajax refresh why not just have the ajax referesh be what loads initially? then the refresh si built in.

Thank you, guys.

In this case data come from database. So is good practise add this helper method to model? For data from another source - is any common place where to store these helper classes?

Ajax refresh will be as

$('#thing').html(newHtml)

Reason why I can not initiate everything with "ajax refresh" is I need to be independent of Javascript. Page must work also without JS.



145.0k
Accepted
answer

You can put it as well to service maybe, create repository class or just put static method to model or just use ::find(). That's why you should FIRST check docs and then use any framework, not write question first without even reading docs.

Wojciech, why are you responding me when you dislike it? :) Why do you say things I do not know anything about OOP, I didnt check docs and write post first etc. etc.?

I just asked for good practise. Of course I tryied to understand docs, have read about models and so on. But I asked here what is best practise for doing my need because I am framework newbie and want to use it right way and another people have some experiences. If you think I missed something for my use in docs, please link it here. For example ::find() way seems not to be good because of it may be too complex database query and I will repeat find params on two or more places. WIll try other suggestions. Thank you.

edited Jun '16

I meant first post here, if you would read first docs before your question then you would know that you can use volt include or partial. Phalcon has really good documentation and you should sure use it.

edited Jun '16

Wojciech, is possible that human does not understand something in docs? I am not king of programmers :) also my English is not as good as I want.

But important thing is - I started this topic with question about some solution for my need. I was doing everything in PHP on my own without any framework until today. While I started to read about using frameworks that are created to do much work for me (or not?) I asked here if is any way better than using partials, create any helper method or service, bind parameters/values to it etc. (thought I will call actions one by one, remember?) If framework has something for it. I could not find it in docs because it can not it be there because I ask something is not possible. Clear? When I received answer that includes or partials are the way, I implemented it and asked only one question about good practise. Because I want use Phalcon best way possible. Invectives degrade your answers however you are expert. And mute newbies enthusiasm.

So, thanks once more for answers, have nice days.