Created
October 31, 2017 01:02
-
-
Save marcosfreitas/33f2076b5115fd30610fa66e5fc941ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controllers\Site\Ads; | |
use Silex\Application; | |
use Symfony\Component\HttpFoundation\Request; | |
use Silex\Api\ControllerProviderInterface; | |
class AdsControllerProvider implements ControllerProviderInterface { | |
public function connect(Application $app) { | |
// creates a new controller based on the default route | |
$controller = $app['controllers_factory']; | |
# list adversements | |
$controller->get('/{type}/{page}/', '\App\Controllers\Site\Ads\Ads::list') | |
->convert('page', function($page){ | |
if ($page === '0') { | |
$page = '1'; | |
} | |
return $page; | |
}) | |
->assert('page', '\d+'); // only digit | |
$controller->get('/{type}/', function($type) use ($app) { | |
return $app->redirect(SITE_URL.'anuncios/'.$type.'/1/', 302); | |
}); | |
# load a post | |
$controller->get('/{type}/v/{code}/', '\App\Controllers\Site\Ads\Ads::load') | |
->convert('code', function($code, $app){ | |
if (empty($code)) { | |
return $app->redirect(SITE_URL.'anuncios/'.$type.'/1/', 302); | |
} | |
return $code; | |
}) | |
->assert('code', '([a-zA-Z0-9]){6,6}'); // only not digit | |
return $controller; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment