# Importing Koseven as a Library If you're working with an existing codebase it's often difficult to modernise the code as it would mean a complete rewrite and there's rarely the time. An alternative is to improve the codebase incrementally as best you can, gradually outsourcing code to external libraries to reduce the amount of old code there is to maintain. This tutorial describes how to include the Koseven PHP framework into existing PHP applications, without having to use the routing and HMVC request handling features. In normal usage of the Koseven framework, the `index.php` file acts as the request handler; it sets up the environment, loads the system configuration, and then handles the request (see [Request Flow](flow)). We'll walk you through the steps required to create a file we'll call `include.php` which will allow you to include Koseven from exiting PHP applications. ## Demo application The following file will serve as our (insultingly simple) demo application for this tutorial. ### File: `demo.php` ~~~ Demo page ~~~ ## Install Koseven [Download and install the Koseven framework](install); from this point on, we'll be referring to the location of the Koseven libraries as the `ko7` directory. ## Create a common setup file Since `index.php` and `include.php` will duplicate a lot of code, we're going to move that code to a third file, `common.php`. The bulk of the code is unchanged; we've changed the install check to exit rather than return after rendering, and removed the request execution. The new file creates the initial request object, rather than fully executing the request, so that, if you do define routes, the `Request::$initial` variable will be set up correctly. ### File: `ko7/common.php` ~~~ execute() ->execute() ->send_headers(TRUE) ->body(); ~~~ ## Create the include file Our `include.php` file is also pretty simple. The try-catch clause is needed because if the request matches no routes Koseven will throw an `HTTP_Exception_404` exception. ### File: `ko7/include.php` ~~~ Demo page

~~~