This repository has been archived by the owner on Sep 19, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e299e6
commit dcec0e0
Showing
6 changed files
with
173 additions
and
48 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Copyright 2014 Krzysztof Magosa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
namespace Site\Controller; | ||
|
||
use KM\Saffron\RoutingResult; | ||
|
||
class ErrorController | ||
{ | ||
public function notFoundAction(RoutingResult $result) | ||
{ | ||
header($_SERVER["SERVER_PROTOCOL"] .' 404 Not Found'); | ||
echo 'Error 404'; | ||
} | ||
|
||
public function methodNotAllowedAction(RoutingResult $result) | ||
{ | ||
header($_SERVER["SERVER_PROTOCOL"] .' 405 Method Not Allowed'); | ||
// This header is required by RFC 2616 | ||
header('Allow: ' . implode(', ', $result->getAllowedMethods())); | ||
|
||
echo 'Error 405<br>'; | ||
echo 'Try with one of them: ' . implode(', ', $result->getAllowedMethods()); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,61 @@ | ||
<?php | ||
/** | ||
* Copyright 2014 Krzysztof Magosa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use KM\Saffron\Request; | ||
use KM\Saffron\Executor; | ||
use KM\Saffron\RouterFactory; | ||
|
||
$factory = new RouterFactory( | ||
function ($collection) { | ||
$collection->route('home') | ||
->setUri('/') | ||
->setTarget('Site\Controller\HomeController'); | ||
// Build request object based on superglobals | ||
$request = Request::createFromGlobals(); | ||
|
||
$collection->route('product') | ||
->setUri('/product/{slug}/{id}') | ||
->setTarget('Site\Controller\ProductController') | ||
->setRequires( | ||
[ | ||
'slug' => '\w+', | ||
'id' => '\d+', | ||
] | ||
); | ||
} | ||
); | ||
// Load routes from external file to keep this file clean | ||
$factory = new RouterFactory(require __DIR__ . '/routes.php'); | ||
|
||
// Set parameters for factory and build router | ||
// Make sure that web server can write to cache dir. | ||
// If cache dir is used for many projects you are obliged | ||
// to set unique class suffix in each of them. | ||
$router = $factory | ||
->setCacheDir(__DIR__ . '/../cache') | ||
->setClassSuffix('Example') | ||
->build(); | ||
|
||
$router = $factory->build(); | ||
$route = $router->match(\KM\Saffron\Request::createFromGlobals()); | ||
// Match request against configured routes | ||
$result = $router->match($request); | ||
|
||
if ($route) { | ||
$executor = new Executor($route); | ||
if ($result->isSuccessful()) { | ||
// Request matched route, run the controller | ||
$executor = new Executor($result); | ||
$executor->fire(); | ||
} | ||
else { | ||
echo 'Error 404'; | ||
} else { | ||
$executor = new Executor(); | ||
$executor | ||
->setController('Site\\Controller\\ErrorController') | ||
->setParameters(['result' => $result]); | ||
|
||
if ($result->isResourceNotFound()) { | ||
// Request didn't matched any route | ||
$executor | ||
->setMethod('notFoundAction') | ||
->fire(); | ||
} elseif ($result->isMethodNotAllowed()) { | ||
// Request matched route, but method is not allowed | ||
$executor | ||
->setMethod('methodNotAllowedAction') | ||
->fire(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Copyright 2014 Krzysztof Magosa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// | ||
// Below Closure is called only when cache is empty. | ||
// When you change something here, you need to empty cache directory. | ||
// | ||
return function ($collection) { | ||
/** | ||
* // You will use name later to generate links. | ||
* // Each name has to be unique. | ||
* $collection->route('name') | ||
* ->setUri('/some/route/with/{parameter1}/and/{parameter2}') | ||
* ->setDomain('www.example.{tld}') | ||
* ->setMethod('GET') - you can also pass array with more methods | ||
* ->setHttp(false) - resource is accessible only by NON-https connection | ||
* ->setRequirements( | ||
* [ | ||
* 'parameter1' => '\w+', // parameter1 must be alphanumeric | ||
* 'parameter2' => '\d+', // parameter2 must be a number | ||
* 'tld' => 'com|org', // tld in domain must be com or org | ||
* ] | ||
* ) | ||
* ->setDefaults( | ||
* [ | ||
* 'parameter2' => 'value2', // when link doesn't contain parameter2, it has 'value2' | ||
* ] | ||
* ) | ||
* ->setTarget('HomeController', 'indexAction'); // you can omit action, the default is 'indexAction' | ||
*/ | ||
|
||
$collection->route('home') | ||
->setUri('/') | ||
->setMethod(['POST', 'PUT']) | ||
->setTarget('Site\Controller\HomeController'); | ||
|
||
$collection->route('product') | ||
->setUri('/product/{slug}/{id}') | ||
->setTarget('Site\Controller\ProductController') | ||
->setRequirements( | ||
[ | ||
'slug' => '\w+', | ||
'id' => '\d+', | ||
] | ||
); | ||
}; |