### Running PHP Built-in Web Server Source: https://github.com/nramenta/bento/blob/master/examples/blog/README.md This command starts PHP's built-in web server, serving the application from `index.php` on `localhost` port `8000`. It provides a quick way to run the application for development or testing without requiring a separate web server like Apache or Nginx. ```Bash php -S localhost:8000 index.php ``` -------------------------------- ### Installing Bento via Composer - JSON Source: https://github.com/nramenta/bento/blob/master/README.md This JSON snippet shows the minimum `composer.json` configuration required to install Bento using Composer. It specifies the `bento/bento` package at its stable version as a dependency, allowing for easy integration into PHP projects. ```JSON { "require": { "bento/bento": "@stable" } } ``` -------------------------------- ### Recreating SQLite Database Schema Source: https://github.com/nramenta/bento/blob/master/examples/blog/README.md This command recreates the `data.db` SQLite database by piping the `schema.sql` file into the `sqlite3` command-line interface. This process is necessary if the `data.db` file is missing or corrupted, ensuring the database schema is correctly initialized. ```Bash sqlite3 data.db < schema.sql ``` -------------------------------- ### Starting PHP Built-in Server - Shell Source: https://github.com/nramenta/bento/blob/master/README.md This shell command invokes PHP's built-in web server, available since PHP 5.4. It starts a server on `localhost:8000` and uses `index.php` as the front controller for all requests, providing a simple way to test the application locally without a full web server. ```Shell php -S localhost:8000 index.php ``` -------------------------------- ### Implementing 'Hello, World!' Route - PHP Source: https://github.com/nramenta/bento/blob/master/README.md This PHP snippet demonstrates the basic usage of Bento's routing system. It requires the Bento framework, defines a GET route for `/hello/`, and echoes a greeting using the `e()` helper for HTML escaping. The `return run(__FILE__)` call is crucial for compatibility with PHP's built-in server. ```PHP ', function($world) { echo 'Hello, ' . e($world); }); return run(__FILE__); ``` -------------------------------- ### Configuring Nginx for Subdirectory - Nginx Source: https://github.com/nramenta/bento/blob/master/README.md This Nginx configuration block is specifically for Bento applications installed in a subdirectory (e.g., `/myapp`). It uses `try_files` to route requests within that subdirectory to its `index.php` file, preserving the query string and ensuring correct application bootstrapping. ```Nginx location /myapp { try_files $uri $uri/ /myapp/index.php?$query_string; } ``` -------------------------------- ### Configuring Apache for Subdirectory - Apache Source: https://github.com/nramenta/bento/blob/master/README.md This Apache `.htaccess` configuration handles `mod_rewrite` for applications installed in a subdirectory. It dynamically appends the `REWRITE_BASE` environment variable to ensure requests are correctly routed to `index.php` within the specific subdirectory, accommodating non-root deployments. ```Apache RewriteEngine On # append %{ENV:REWRITE_BASE} to rules. RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$ RewriteRule ^(.*)$ - [E=REWRITE_BASE:%1] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ %{ENV:REWRITE_BASE}index.php [L] ``` -------------------------------- ### Implementing Post-Redirect-Get Pattern in PHP Source: https://github.com/nramenta/bento/blob/master/README.md This snippet demonstrates the Post-Redirect-Get (PRG) pattern using Bento's helper functions. It handles form submissions by updating a post, flashing success or error messages, and then redirecting to prevent duplicate submissions. If the request is GET, it displays the form. ```PHP ', function($id) { if (request_method('POST')) { // update the post if ($success) { flash('notice', 'Post successfully saved. Yay!'); } else { flash('error', 'Something went horribly wrong :('); } redirect(); // redirect to the current request path } // display form if the request method is GET display_template('post_edit.html', array( 'notice' => flash('notice'), 'error' => flash('error'), 'csrf_field' => csrf_field() )); }); ``` -------------------------------- ### Defining Route with Trailing Slash - PHP Source: https://github.com/nramenta/bento/blob/master/README.md This snippet defines a GET route `/books/` that responds with 'Books!'. It demonstrates Bento's automatic 301 redirection behavior where requests to `/books` (without a trailing slash) will be redirected to `/books/`. ```PHP ', function($id) { // auth() here is a custom user-defined authorization function auth() and prevent_csrf(); // continue ... }); ``` -------------------------------- ### Generating CSRF Query String for GET Requests - PHP Source: https://github.com/nramenta/bento/blob/master/README.md This snippet shows how to generate a URL-encoded query string with the CSRF token using `csrf_qs()`. This is useful for protecting idempotent GET requests, such as a delete link, by embedding the token directly into the URL. ```PHP ', function($id) { echo 'delete post'; }); ``` -------------------------------- ### Configuring Nginx for Root Directory - Nginx Source: https://github.com/nramenta/bento/blob/master/README.md This Nginx configuration block uses `try_files` to serve static files if they exist, otherwise it rewrites the request to `index.php` with the original query string. This setup is ideal for Bento applications deployed in the root of the Nginx server, ensuring proper routing. ```Nginx location / { try_files $uri $uri/ /index.php?$query_string; } ``` -------------------------------- ### Defining Route Without Trailing Slash - PHP Source: https://github.com/nramenta/bento/blob/master/README.md This snippet defines a GET route `/about` that responds with 'About Us'. It illustrates that requests to `/about/` (with a trailing slash) will result in a '404 Not Found' error, as the framework only redirects from non-trailing slash to trailing slash, not the reverse. ```PHP RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] ``` -------------------------------- ### Autoloading Callbacks as Static Methods in PHP Source: https://github.com/nramenta/bento/blob/master/README.md This snippet shows how to register route callbacks as static methods of a class. This approach leverages PHP's autoloading functionality, ensuring that the callback code is loaded into memory only when the corresponding route is accessed, optimizing performance. ```PHP ', array('Blog', 'get_post')); ``` -------------------------------- ### Defining a Named Route - PHP Source: https://github.com/nramenta/bento/blob/master/README.md This PHP snippet shows how to define a named route using `route_for()`. It assigns the name `user.edit` to the route pattern `/users/`, allowing this route to be referenced by its name later for URL generation or redirection, improving maintainability. ```PHP '); get('user.edit', function() { // display user edit/update form }); ``` -------------------------------- ### Generating URL with Named Route - PHP Source: https://github.com/nramenta/bento/blob/master/README.md This PHP snippet demonstrates how to use a previously defined named route (`user.edit`) with the `url_for()` function. It generates a URL by providing the route name and an associative array of parameters, in this case, an `id` of `42`, abstracting the URL pattern. ```PHP '); url_for('user.edit', array('id' => 42)); ``` -------------------------------- ### Setting and Retrieving Flash Session Variables - PHP Source: https://github.com/nramenta/bento/blob/master/README.md This snippet shows how to set a flash session variable in one request (`/step-1`) and retrieve it in the very next request (`/step-2`). Flash sessions are temporary key-value pairs that are automatically cleared after being accessed once or if another URL is visited. ```PHP ', function($id) { halt('database', 'connection_error'); // return a 404 response if $id is not found halt(404); }); ``` -------------------------------- ### Registering Custom Error Handlers in PHP Source: https://github.com/nramenta/bento/blob/master/README.md This snippet demonstrates how to register custom error handlers using the `error()` function. Handlers can be defined for specific HTTP status codes (e.g., 404, 400) or custom error types (e.g., 'database'), allowing for tailored error responses. ```PHP 404 - Page Not Found'; }); error(400, function($message = null) { echo '

400 - Bad Request

'; if ($message == 'csrf') { echo '

CSRF detected!

'; } }); error('database', function($message = null) { // handle custom database error based on $message }); ``` -------------------------------- ### Preventing CSRF in POST Route Handlers - PHP Source: https://github.com/nramenta/bento/blob/master/README.md This snippet demonstrates how to validate the CSRF token in a POST route handler by calling `prevent_csrf()`. If the token is invalid or missing, the framework automatically halts the request with a '400 Bad Request' error, protecting the application from CSRF attacks. ```PHP