Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
Example
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-magosa committed Oct 19, 2014
1 parent 8e299e6 commit dcec0e0
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 48 deletions.
37 changes: 37 additions & 0 deletions example/src/Controller/ErrorController.php
@@ -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());
}
}
14 changes: 14 additions & 0 deletions example/src/Controller/HomeController.php
@@ -1,4 +1,18 @@
<?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;

class HomeController
Expand Down
14 changes: 14 additions & 0 deletions example/src/Controller/ProductController.php
@@ -1,4 +1,18 @@
<?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;

class ProductController
Expand Down
72 changes: 49 additions & 23 deletions example/src/index.php
@@ -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();
}
}
25 changes: 0 additions & 25 deletions example/src/router.php

This file was deleted.

59 changes: 59 additions & 0 deletions example/src/routes.php
@@ -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+',
]
);
};

0 comments on commit dcec0e0

Please sign in to comment.