Creating an Application
Lithe allows you to create applications in a simple and versatile way, offering two writing styles: classic and functional. You can choose one of the two or even combine both, without losing efficiency!
Classic Style: Instantiating the Application
To get started with the classic style, you'll need to mount the application in the App.php
file using the mount
method from the Lithe\App
class. After that, simply define your routes:
$app = \Lithe\App::mount();
$app->get('/', function ($req, $res) {
$res->send('Hello World!');
});
The App
class is the heart of your Lithe application. It manages all the application logic, making it easy to create routes, handle requests, and responses in an organized way.
Functional Style: Practicality and Agility
If you prefer a more minimalist approach, you can opt for the functional syntax without the need to directly instantiate the class:
use function Lithe\Orbs\Http\Router\get;
get('/', function ($req, $res) {
$res->send('Hello World!');
});
This method is quick, efficient, and allows you to write code in an even more concise manner.
Running the Application
Now that you've written your first route, the next step is to run the application. If you're using the classic way, call the listen
method for Lithe to start listening for requests:
$app = \Lithe\App::mount();
$app->get('/', function ($req, $res) {
$res->send('Hello World!');
});
$app->listen();
With Lithe 2, this method is automatically invoked.
Serving the Application
Using the Built-in PHP Server
Testing your application locally is easy! With PHP installed, you can start the built-in server by executing the following command:
php line serve
This will start the server on port 8000
. To change the port, just pass the desired value as an argument:
php line serve 9000
Open your browser and go to http://localhost:9000/
to see your application in action!
Using Web Servers like Apache
If you prefer to run your application on a web server like Apache, the entry point will be the public
directory. Adjust the URL settings according to your environment:
- Local Environment:
http://localhost/your-project/public/
- Production:
http://your-domain.com/
Make sure the server is pointing to the correct directory and you're good to go! Your application will be available to the world.
With Lithe, simplicity and flexibility are at your fingertips, providing an agile development experience, whether for small or large projects. Start now and build something amazing!