Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
1.9k views
in Q2A Core by

If you have developed some plugins, you might have faced the problem to generate a question url. I mean, not only the pure domain.com/123 but the "offical" URL domain.com/123/question-about-coffee

The way it must be done until today (v1.7 beta 2014-11-25) and all q2a version before:

1. get the question $title

2. use function qa_q_path($questionid, $title) to generate the URL or function qa_q_request()

I always wonder, again and again, why we just don't have a direct:

function qa_q_path($questionid);

And if the title is missing, we do an extra query to get them. As easy as that.


Any thoughts?

Q2A version: 1.7

1 Answer

0 votes
by
selected by
 
Best answer

Without taking into account whether it is possible or not (because it is obviously possible) I think that function shouldn't work that way. The reason is just dependencies.

The qa_q_path() function currently has no dependencies and can return the values it is looking for on it's own (as the rest of the functions in that file, with the exception of the plugin-loading-related functions). I'd leave it just like that. Adding a database access would force the function to depend on the database. That could leave to unexpected errors because the implementation wouldn't be so predictable for developers.

The suggested feature could, however, be implemented just in a helper wrapper. That could be in another file (in general, very originally called, helpers.php). That generic-purpose file could contain a qa_q_path_full() function that would work exactly as you suggest. Furthermore, you don't need that in the core. You could just have that file in your own plugins.

Finally, and maybe I'm just talking about my experience, I don't think there are so many situations in which I wanted to display the full URL and didn't have a question array in context to get the title from. In fact, I've only faced that once, which means, the qa_q_path_full() function would have saved me a line (to fetch the title from the DB) and also would have added a line (to require the helpers.php file).

by
Thanks for the information.

1. Fair point, having another mysql query (maybe even multiple times) would reduce performance. Having the postid stored is "just reading".
2. I probably will create a bunch of helper functions, like I heavily did with the q2apro booker plugin https://github.com/q2apro/q2apro-booker
3. Now I understand why qa_q_path() is actually helpful, own logic, no db access. Thanks!
...