Jump to content

Best way to call PW functions inside a page


dhruba
 Share

Recommended Posts

Hi,

Is there any standard way to call some PHP function inside a page. Like I have created a spacial page that can access by member (has permission for this page). I have three button to   create edit and delete   page (pages only have one title field) so I don't want to make three pages for this. Is ajax is better?

THANKS

Link to comment
Share on other sites

I tend not to use functions but classes with static methods to organize code into contexts.

In my config I place this code for example:

function MZWHelpers($className) {
	$path = wire('config')->paths->root . 'site/templates/_classes/';
	$file = $path . $className . ".php";
	
	if(file_exists($file))	 {
		include $file;
	}	
}
spl_autoload_register('MZWHelpers', true);

I can call them in my templates via:

MZWHelpers::saySomething("hello");

The class gets automatically loaded and included in every template whenever I call methods of that class, so very convenient.

If you add other classes to abstract/collect specific logic, you just add another autoload in the config for that class.

Anyway, that's how I do it, perhaps there's a better way.

  • Like 3
Link to comment
Share on other sites

Actions that create, modify or delete an entry in the DB (like save) should be done via POST requests. I find urlSegments or GET variables handy for the rest. Here's a rough skeleton of how you could do it:

function edit() { ... }
function save() { ... }
function delete() { ... }

if($input->urlSegment1 == 'edit') edit(); 
   else if($input->post->save) save(); 
   else if($input->post->delete) delete(); 
   else $session->redirect($page->url . 'edit'); 
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...