Search the Community
Showing results for tags 'clean code'.
-
Hello, this feature wish came up while I was working on my first bigger project with ProcessWire. While almost all of the CMS is very well thought out there was one thing that always kept me thinking "where should I put this?": simple page actions. What I mean is code snippets that belong to a page and should be run when a certain action is taken. E.g. when a user submits the form on my contact page (that has the contact template) I somewhere have to validate the form and then send it somewhere. So far I see two options to do that. Write a module that hooks into some methods and checks if the post data is set and if the page is my contact page This aproach is clean but it's usually a lot of overhead to write a module just for that Write the code in my contact.php template This aproach is fast but feels really dirty. It does not help to separate the logic from the design which I fear in a long term could lead to some Wordpressish code structure Now since I don't have so much experience with ProcessWire there might be a chance I missed something, but then it's probably not documented obvious enough. So I thought how you improve on this problem and came to a solution, that's again really close to the syntax that jQuery provides. If we had some kind of routing system we could register those actions in any file, for example the templates _func.php <?php // execute this action when a post request is sent to /contact // the parameters given to the callback function probably should be different wire('request')->on('post', '/contact', function($request, $input) { // do an action } // do the action for any post request wire('request')->on('post', function($request, $input) { // do an action } // do the action for get requests on contact pages wire('request')->on('get', 'template=contact', function($request, $input) { // do an action } // do the action on ajax-GET requests wire('request')->on('get.ajax', '/contact', function($request, $input) { // do an action } Is that something that could improve the work with ProcessWire? What's your honest opinion about it? If that's a useful suggestion I would like to help with the implementation. Regards Alex