PHPackages                             codermarcel/simple-controller - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. codermarcel/simple-controller

ActivePackage[Framework](/categories/framework)

codermarcel/simple-controller
=============================

Convenient and simple silex controller using reflection

v0.1.1(8y ago)8461MITPHP

Since Oct 30Pushed 8y ago1 watchersCompare

[ Source](https://github.com/codermarcel/SimpleController)[ Packagist](https://packagist.org/packages/codermarcel/simple-controller)[ RSS](/packages/codermarcel-simple-controller/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

\#SimpleController

SimpleController is a convenient reflection based controller for the \[php micro-framework silex\] () SimpleController makes it easy for you to use controllers in your silex applications and matches your controller methods to routes automatically

\#Installation

Run the following command:

```
composer require codermarcel/simple-controller
```

Setup
=====

[](#setup)

Method - 1
----------

[](#method---1)

### Extending the SimpleController

[](#extending-the-simplecontroller)

```
use Codermarcel\SimpleController\SimpleController;;

class MyExampleControllerExtended extends SimpleController
{
	/**
	 * Responds to requests to GET /
	 */
	public function getIndex()
	{
		return echo 'Welcome!';
	}
}
```

### Mount the route

[](#mount-the-route)

```
$app->mount('/', new App\Controllers\MyExampleControllerExtended());
```

Method - 2
----------

[](#method---2)

### Using a raw class

[](#using-a-raw-class)

If you don't want to extend the SimpleController class, then you can use a raw class as well.

```
class MyExampleControllerRaw
{
	/**
	 * Responds to requests to GET /
	 */
	public function getIndex()
	{
		return echo 'Welcome!';
	}
}
```

### Mount the route

[](#mount-the-route-1)

**Note** use the full namespace name for your controller class

```
$app->mount('/', new Codermarcel\SimpleController\SimpleController('App\Controllers\MyExampleControllerRaw'));
```

\#Usage

HTTP methods
------------

[](#http-methods)

The method names should begin with the HTTP verb they respond to followed by the route name.
The following methods are available :

- get
- post
- put
- delete
- patch
- options
- match

#### below are some examples

[](#below-are-some-examples)

```
class MyExampleControllerRaw
{
    /**
     * Responds to requests to GET /test
     */
    public function getTest()
    {
        //
    }

    /**
     * Responds to requests to GET /show/{id}
     */
    public function getShow($id)
    {
        //
    }

    /**
     * Responds to requests to GET /admin-profile
     */
    public function getAdminProfile()
    {
		//
    }

    /**
     * Responds to requests to POST /profile
     */
    public function postProfile()
    {
		//
    }

}
```

### Organizing Controllers

[](#organizing-controllers)

> When your application starts to define too many controllers, you might want to group them logically:

> mount() prefixes all routes with the given prefix and merges them into the main Application. So, / will map to the main home page, /blog/ to the blog home page, and /forum/ to the forum home page.

For more information on Organizing Controllers, please take a look at the offical \[silex documentation\] ([http://silex.sensiolabs.org/doc/organizing\_controllers.html#organizing-controllers](http://silex.sensiolabs.org/doc/organizing_controllers.html#organizing-controllers))

#### Example 1

[](#example-1)

```
$app->mount('/', new App\Controllers\MyExampleControllerExtended());
```

```
use Codermarcel\SimpleController\SimpleController;;

class MyExampleControllerExtended extends SimpleController
{
	/**
	 * Responds to request to GET /
	 */
	public function getIndex()
	{
		//
	}

	/**
	 * Responds to request to GET /login-page
	 */
	public function getLoginPage()
	{
		//
	}
}
```

#### Example 2

[](#example-2)

```
$app->mount('/user', new App\Controllers\MyExampleControllerExtended());
```

```
use Codermarcel\SimpleController\SimpleController;;

class MyExampleControllerExtended extends SimpleController
{
	/**
	 * Responds to request to GET /user/
	 */
	public function getIndex()
	{
		//
	}

	/**
	 * Responds to request to GET /user/home-page
	 */
	public function getHomePage()
	{
		//
	}
}
```

### Route variables

[](#route-variables)

You can define variable parts in a route like this:

**Note** default route values are currently not supported but might be added in a later version.

```
class MyExampleControllerRaw
{
	/**
	 * Responds to requests to POST /login{username}/{password}
	 */
	public function postLogin($username, $password)
	{
		return sprintf('Trying to log in with username: %s and password: %s', $username, $password);
	}
}
```

### Request and Application injection

[](#request-and-application-injection)

You can also ask for the current Request and Application objects like this:

**Note** silex does the injection based on the type hinting and not on the variable name!

```
class MyExampleControllerRaw
{
	use Silex\Application;
	use Symfony\Component\HttpFoundation\Request;

	/**
	 * Responds to requests to GET /injection
	 */
	public function getInjection(Application $app, Request $request)
	{
		//
	}
}
```

### Named routes

[](#named-routes)

You can bind a route name to your routes by using the **$bind** parameter in your routes.

For more information on named route and the UrlGeneratorServiceProvider please take a look at the \[offical silex documentation\] ([http://silex.sensiolabs.org/doc/providers/url\_generator.html#urlgeneratorserviceprovider](http://silex.sensiolabs.org/doc/providers/url_generator.html#urlgeneratorserviceprovider))

```
class MyExampleControllerRaw
{
	use Silex\Application;
	use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

	/**
	 * Responds to requests to GET /bind-example
	 *
	 * {@link http://silex.sensiolabs.org/doc/providers/url_generator.html#usage}
	 */
	public function getBindExample(Application $app, $bind = 'bind_example')
	{
		//Example usage of the bind_example route
		//You can use ABSOLUTE_URL or ABSOLUTE_PATH
		return new Response($app['url_generator']->generate('bind_example', array(), UrlGeneratorInterface::ABSOLUTE_PATH));
	}
}
```

Middleware
----------

[](#middleware)

> Silex allows you to run code, that changes the default Silex behavior, at different stages during the handling of a request through middlewares:
> \[…\]
> Route middlewares are triggered when their associated route is matched.

For more information about middlewares, please take a look at the offical \[silex documentation\] ()

**Note** You can typehint the Request, Response or Application object and **silex** will inject them for you.

```
class MyExampleControllerRaw
{
	use Symfony\Component\HttpFoundation\Response;
	use Symfony\Component\HttpFoundation\Request;

	/**
	 * Before middleware example
	 *
	 * {@link http://silex.sensiolabs.org/doc/middlewares.html#before-middleware}
	 */
	public function beforeMiddleware(Request $request)
	{
		if ($request->getRequestUri() === '/before-middleware')
		{
			return new Response('YOU SHALL NOT PASS');
		}
	}

	/**
	 * After middleware example
	 *
	 * {@link http://silex.sensiolabs.org/doc/middlewares.html#after-middleware}
	 */
	public function afterSomeRandomNameThatDoesntMatter(Request $request, Response $response, Application $app)
	{
		if ($request->getRequestUri() === '/after-middleware')
		{
			return new Response($response->getContent() . ' | after-middleware content');
		}
	}
}
```

Credits
-------

[](#credits)

SimpleController was inspired by

And

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~692 days

Total

2

Last Release

3206d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d1f8c0ec8523e47f58aa852a1ee2a57ebb4be3e3a02b5ccb789a88a4c824c986?d=identicon)[codermarcel](/maintainers/codermarcel)

---

Tags

controllersilex

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/codermarcel-simple-controller/health.svg)

```
[![Health](https://phpackages.com/badges/codermarcel-simple-controller/health.svg)](https://phpackages.com/packages/codermarcel-simple-controller)
```

###  Alternatives

[ddesrosiers/silex-annotation-provider

A silex service provider that allows the use of annotations in ServiceControllers.

28251.1k3](/packages/ddesrosiers-silex-annotation-provider)[tobiassjosten/responsible-service-provider

A Silex ServiceProvider for automagic response formatting.

3492.8k](/packages/tobiassjosten-responsible-service-provider)[propel/propel-service-provider

Propel integrationfor Silex.

2725.5k3](/packages/propel-propel-service-provider)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
