Getting URL Parameters in concrete5
It is often necessary to include parameters in URL addresses. For instance, if you want to display an article, you would probably want to include the article’s ID in the URL and then search for this article ID in the database. In concrete5, many make use of PHP’s $_GET superglobal variable. This approach does, however, make URLs look less pretty and attractive, unless they are rewritten of course. Luckily there is a better way to do it.
Let us say that you want to process a page request in a controller where you want to include an article’s ID. You can simply include the parameter in the controller’s method signature like this:
class SomeController extends Controller {
public function view($articleID) {
// Process request
}
}
If the URL that will map to the method above is mydomain.com/something, then mydomain.com/something/123 will execute the method above with a value of 123 in $articleID. In the current form, the parameter is required, meaning that an error will occur if it does not exist. However, you may use the PHP syntax you are probably already familiar with to use optional parameters. Let us expand the above example slightly to include an optional article title that may be used for SEO purposes.
class SomeController extends Controller {
public function view($articleID, $articleTitle = null) {
// Process request
}
}
If we do not provide a title, the method is still executed. We can then simply check to see if the $articleTitle parameter is not null to use it for any purpose.
There are many scenarios for using this, but the principle remains the same, whether you need to do it with blocks or single pages.