You are faced with the challenge of representing Routing in an object-oriented PHP application accordingly? Routing is a central part of any web application, as it determines how incoming URLs are assigned to the appropriate logic components, namely controllers and their actions. This guide will help you understand and implement the routing process in PHP by detailing the assignment of controllers and actions.
Key Insights
- Routing allows the mapping of incoming URL requests to specific controller methods.
- The naming of controllers and methods follows certain naming conventions to ensure consistency.
- With PHP Reflection, you can check whether a specific controller method exists before calling it.
- Parameters need to be carefully processed and passed to the corresponding methods in an associative array.
Step-by-Step Guide
Step 1: Create Directory Structure and Namespace
First, you create a new directory named Controller, where you will place your controller classes. It is important to organize the classes into meaningful namespaces. For example:

Next, you define the namespace for the controller. It could look like this:
Step 2: Define Controller Class
In this step, you create a class for your desired controller. Let's call it UploadController. For this, you need some basic settings to initialize the class. For example:

Here you should ensure that you format all controller names in lowercase to avoid subsequent confusion. This not only helps with clarity but also allows you to maintain the standardization process in your application.
Step 3: Create Controller Instance
Now you create an instance of your controller. This is typically done through a string formatting that includes the full name of the class – including the namespace. For example:

To make it easy to implement in the further logic, check if the class exists before proceeding with further steps. For this, you use a simple if statement to verify the existence of the class.
Step 4: Exception Handling for Missing Controllers
If the controller does not exist, it is advisable to trigger an exception. In this case, use the InvalidArgumentException to clearly inform the user that the specified controller is unknown:

Here you could, for example, output a notice like:
Controller unknown: [Controller-Name]
Step 5: Shared Parameters for the Controller Action
The next step is to define the actions and their methods for the controller. In this example, we establish that there is a method named save that is responsible for the upload. In doing so, you instruct PHP to integrate this method a bit into the controller. This is done via string formatting, similar to the controllers:

This ensures that external calls are only accepted by authorized action methods.
Step 6: Verify Action Method Using Reflection
To ensure that the specified method exists, PHP’s Reflection is now used. This is done by instantiating the ReflectionClass.

Here, you check with the hasMethod method whether the desired action method actually exists in your controller:
Step 7: Implement Parameter Access Logic
After specifying the controllers and action, it's time to process the incoming parameters. These parameters typically appear in the form of a URL segment and must be split into pairs. For this, you use the explode() function:

In this step, you must also ensure that the number of parameters is even. If not, there is an invalid number of parameters – again, an InvalidArgumentException is recommended:
Step 8: Create an Associative Array for the Parameters
In the next step, you convert the parameters into an associative array so that you can easily pass them to the action. You can do this through a loop that specifically maps each key-value pair:

Here, you alternate between taking keys and values to make an assignment before passing the final array to your controller method.
Step 9: Call Controller Action
Now it gets practical: You are ready to call your controller's method with the corresponding parameters you defined earlier. To do this, you instantiate the controller and call the method as follows:

Make sure all necessary steps have been executed correctly. You have now successfully implemented routing in your PHP application.
Summary
In this text guide, you learned how to implement routing through controllers and actions in an object-oriented PHP application. Starting from the directory structure to defining and testing controllers and their methods, and dealing with parameters – all this enables you to create a powerful routing system for your web application.
Frequently Asked Questions
How do I implement routing in PHP?Routing is organized by mapping URLs to controllers and their action methods.
What are controllers in PHP?Controllers are classes that control the logic for processing requests and calling the corresponding action methods.
How can I ensure that an action method exists?Use PHP Reflection to check if the method is present in the corresponding controller class.
How do I process URL parameters?Parameters should occur in even pairs and can be converted into an associative array using explode().
What happens if my controller does not exist?You should throw an appropriate exception to indicate that the controller is unknown.