mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-15 18:23:26 +03:00
Development
- Made easier to extend. - Added IRoute class. - Changed namespace for Route classes. - Moved find-url related stuff to Route classes itself. - Added more tests for finding urls. - Added support for custom names on RouteController and RouteResource. - Removed depricated methods. - Updated documentation. - Updated demo-project to reflect changes. - Other small bugfixes and improvements.
This commit is contained in:
@@ -6,7 +6,7 @@ use Pecee\Http\Request;
|
||||
|
||||
class DummyMiddleware implements IMiddleware
|
||||
{
|
||||
public function handle(Request $request, \Pecee\SimpleRouter\RouterEntry &$route = null)
|
||||
public function handle(Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route)
|
||||
{
|
||||
throw new MiddlewareLoadedException('Middleware loaded!');
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
class ExceptionHandler implements \Pecee\Handlers\IExceptionHandler
|
||||
{
|
||||
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\RouterEntry &$route = null, \Exception $error)
|
||||
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
|
||||
{
|
||||
throw $error;
|
||||
}
|
||||
|
||||
@@ -85,17 +85,11 @@ class GroupTest extends PHPUnit_Framework_TestCase
|
||||
// Test method name
|
||||
SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setName('fancy2');
|
||||
|
||||
// Test multiple names
|
||||
SimpleRouter::get('/my/fancy/url/3', 'DummyController@start', ['as' => ['fancy3', 'fancy4']]);
|
||||
|
||||
SimpleRouter::start();
|
||||
|
||||
$this->assertEquals('/my/fancy/url/1/', SimpleRouter::getRoute('fancy1'));
|
||||
$this->assertEquals('/my/fancy/url/2/', SimpleRouter::getRoute('fancy2'));
|
||||
|
||||
$this->assertEquals('/my/fancy/url/3/', SimpleRouter::getRoute('fancy3'));
|
||||
$this->assertEquals('/my/fancy/url/3/', SimpleRouter::getRoute('fancy4'));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,60 +23,78 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase
|
||||
// Match normal route on alias
|
||||
SimpleRouter::get('/', 'DummyController@silent', ['as' => 'home']);
|
||||
|
||||
SimpleRouter::group(['prefix' => '/admin'], function() {
|
||||
SimpleRouter::get('/about', 'DummyController@about');
|
||||
|
||||
SimpleRouter::group(['prefix' => '/admin', 'as' => 'admin'], function() {
|
||||
|
||||
// Match route with prefix on alias
|
||||
SimpleRouter::get('/{id?}', 'DummyController@start', ['as' => 'admin.home']);
|
||||
SimpleRouter::get('/{id?}', 'DummyController@start', ['as' => 'home']);
|
||||
|
||||
// Match controller with prefix and alias
|
||||
SimpleRouter::controller('/users', 'DummyController', ['as' => 'admin.users']);
|
||||
SimpleRouter::controller('/users', 'DummyController', ['as' => 'users']);
|
||||
|
||||
// Match controller with prefix and NO alias
|
||||
SimpleRouter::controller('/pages', 'DummyController');
|
||||
|
||||
});
|
||||
|
||||
SimpleRouter::group(['prefix' => 'api', 'as' => 'api'], function() {
|
||||
|
||||
// Match resource controller
|
||||
SimpleRouter::resource('phones', 'DummyController');
|
||||
|
||||
});
|
||||
|
||||
SimpleRouter::controller('gadgets', 'DummyController', ['names' => ['getIphoneInfo' => 'iphone']]);
|
||||
|
||||
// Match controller with no prefix and no alias
|
||||
SimpleRouter::controller('/cats', 'CatsController');
|
||||
|
||||
// Pretend to load page
|
||||
SimpleRouter::start();
|
||||
|
||||
$this->assertEquals('/gadgets/iphoneinfo/', $this->getUrl('gadgets.iphone'));
|
||||
|
||||
$this->assertEquals('/api/phones/create/', $this->getUrl('api.phones.create'));
|
||||
|
||||
// Should match /
|
||||
$this->assertEquals($this->getUrl('home'), '/');
|
||||
$this->assertEquals('/', $this->getUrl('home'));
|
||||
|
||||
// Should match /about/
|
||||
$this->assertEquals('/about/', $this->getUrl('DummyController@about'));
|
||||
|
||||
// Should match /admin/
|
||||
$this->assertEquals($this->getUrl('DummyController@start'), '/admin/');
|
||||
$this->assertEquals('/admin/', $this->getUrl('DummyController@start'));
|
||||
|
||||
// Should match /admin/
|
||||
$this->assertEquals($this->getUrl('admin.home'), '/admin/');
|
||||
$this->assertEquals('/admin/', $this->getUrl('admin.home'));
|
||||
|
||||
// Should match /admin/2/
|
||||
$this->assertEquals($this->getUrl('admin.home', ['id' => 2]), '/admin/2/');
|
||||
$this->assertEquals('/admin/2/', $this->getUrl('admin.home', ['id' => 2]));
|
||||
|
||||
// Should match /admin/users/
|
||||
$this->assertEquals($this->getUrl('admin.users'), '/admin/users/');
|
||||
$this->assertEquals('/admin/users/', $this->getUrl('admin.users'));
|
||||
|
||||
// Should match /admin/users/home/
|
||||
$this->assertEquals($this->getUrl('admin.users@home'), '/admin/users/home/');
|
||||
$this->assertEquals('/admin/users/home/', $this->getUrl('admin.users@home'));
|
||||
|
||||
// Should match /cats/
|
||||
$this->assertEquals($this->getUrl('CatsController'), '/cats/');
|
||||
$this->assertEquals('/cats/', $this->getUrl('CatsController'));
|
||||
|
||||
// Should match /cats/view/
|
||||
$this->assertEquals($this->getUrl('CatsController', 'view'), '/cats/view/');
|
||||
$this->assertEquals('/cats/view/', $this->getUrl('CatsController', 'view'));
|
||||
|
||||
// Should match /cats/view/
|
||||
$this->assertEquals($this->getUrl('CatsController', ['view']), '/cats/view/');
|
||||
//$this->assertEquals('/cats/view/', $this->getUrl('CatsController', ['view']));
|
||||
|
||||
// Should match /cats/view/666
|
||||
$this->assertEquals($this->getUrl('CatsController@view', ['666']), '/cats/view/666/');
|
||||
$this->assertEquals('/cats/view/666/', $this->getUrl('CatsController@getView', ['666']));
|
||||
|
||||
// Should match /funny/man/
|
||||
$this->assertEquals($this->getUrl('/funny/man'), '/funny/man/');
|
||||
$this->assertEquals('/funny/man/', $this->getUrl('/funny/man'));
|
||||
|
||||
// Should match /?jackdaniels=true&cola=yeah
|
||||
$this->assertEquals($this->getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah']), '/?jackdaniels=true&cola=yeah');
|
||||
$this->assertEquals('/?jackdaniels=true&cola=yeah', $this->getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah']));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user