Using klein.php without composer?

2.3k Views Asked by At

Is there any code example? Here's what I got:

// index.php

require_once __DIR__ . '/Klein/Klein.php';

$klein = new \Klein\Klein();

$klein->respond(function () {
    return 'All the things';
});

On PHP 5.3 this returns an error (Fatal error: Class 'Klein\ServiceProvider' not found in).

2

There are 2 best solutions below

1
On BEST ANSWER

Okay, I got some code together that seems to work. Can't say I recommend it, but it loads Klein without errors, at least for now.

<?php

function include_dir($path) {
    if(is_dir($path)) {
        foreach (glob($path.'*') as $filename) {
            if(is_file($filename) && pathinfo($filename, PATHINFO_EXTENSION) == 'php') {
                require_once $filename;
            } elseif(is_dir($filename)) {
                include_dir($filename.'/');
            } 
        }
    }
}

require_once __DIR__ . '/Klein/Exceptions/KleinExceptionInterface.php';
require_once __DIR__ . '/Klein/Exceptions/HttpExceptionInterface.php';
include_dir(__DIR__ . '/Klein/');

$klein = new \Klein\Klein();

$klein->respond(function () {
    return 'All the things';
});

I started with a little loop on the Klein source directory, I picked up from here: https://stackoverflow.com/a/599694/1004008

However, the code has internal dependencies which expect to be fulfilled by an autoloader. Those files will be included by the loop thing up there, but not in the order desired. So I manually included the two exception interfaces above, before running the loop. It's a little brittle, because the dev could rearrange those dependencies at any time, but it does work.

The more correct answer would be to use an autoloader. I'm not sure why you don't like Composer's autoloader, but it's pretty nifty and easy to use. The autoload stuff is cached, so it doesn't really impact performance. You can use the autoloader independent of the package manager. Composer doesn't dictate your project structure much , other than placing the composer.json and vendors/ directory in the project root. Meanwhile, you can automatically install all sorts of PHP libraries with Composer, lock the version down, or grab updates. It's a nice tool. If you have shared hosting, you can run Composer on your project locally, and then upload the results. Sorry, I'm sure you have your reasons, it's just that Composer is quite possibly the coolest thing to happen in PHP-land in many years.

But if you don't like Composer, or can't use it because of some external constraint, there are other PSR-0/PSR-4 autoloaders:

https://gist.github.com/jwage/221634

https://stackoverflow.com/a/12836587/1004008

Or you can roll your own: http://zaemis.blogspot.fr/2012/05/writing-minimal-psr-0-autoloader.html

Looks like it's only a few lines of code.

Failing that, yeah, I'd probably look for another routing library. Maybe try GluePHP?

http://gluephp.com/

It's a single file, with pretty much zero dependencies. You don't get the trendy Sinatra-style closure-based routing. Instead you create a very simple class for each route. Not sure if that's negotiable for you. But it is small and self-contained.

Slim Framework might be an option as well:

http://www.slimframework.com/

They offer Composer and non-Composer installation methods. The non-Composer method uses its own autoloader. Not a bad approach, but I'm not sure if meets your criteria.

0
On

If you don't want to load with composer, you might want to take a look another library...

I am currently developing a php router which is targeted at extreme high performance. and no extra dependency. you probably might want to take a look:

https://github.com/c9s/Pux

FYI:

Pux is 48.5x faster than symfony router in static route dispatching, 31x faster in regular expression dispatching. (with pux extension installed)

Pux tries not to consume computation time to build all routes dynamically (like Symfony/Routing). Instead, Pux compiles your routes to plain PHP array for caching, the compiled routes can be loaded from cache very fast.

With Pux PHP Extension support, you may load and dispatch the routes 1.5~2x faster than pure PHP Pux.