Skip to main content

PHP routing library for in laravel8 fast API development 2021

What it is? | Laravelnote

PHP Routing is a custom written routing library that was inspired by the Slim framework. It was written as a small and lightweight alternative to it as I needed to use something with as little overhead as possible . The first version of the library was consisting of a single PHP class that was able to handle static and some basic dynamic routes and had a semi working support for adding and using middlewares.

At first that was more than enough for my use case and the library was left at that, but recently I started rewriting one of my personal projects Did You Buy It? live on my Twitch channel and decided I wanted to use PHP for it, as the original API was developed using Node and Express.

What does it do? | Laravelnote

Exaxmple on how to add your own routes:

$routes = new Routes;

$routes->add('/', function() { echo "Hello world"; }, Routes::GET); // If the request method is not specified the GET method is used by default

$routes->add(
    '/users/{int:userID}/posts/{bool:isPublished}',
    function(Request $request, int $userID, bool $isPublished){
        //Do something
    },
    [Routes::GET, Routes::POST]
);

$routes->route();

Why did I build it?

At my day job we needed to develop an API and I've never really tried to build one using vanilla PHP, but instead used frameworks such as CodeIgniter or Slim or just a simple file that would mimic the functionality of an API but not be a proper one. Also we had a requirement that we needed to have full control over the code so we were not supposed to use any of the pre built solutions because of that requirement.

I thought I would look into how to build my own routing library and implement it for our use case. I started of by taking a look at the source code of Slim framework but also just by looking around the web for examples of what other people had done before. One of the video tutorials that really inspired me and I used it as guide while developing this was the one from Codecourse on youtube on how to build your own MVC app.



Popular posts from this blog

Laravel8 in Serializes Models trait | laravelnote

This article was originally posted, with additional formatting, on my personal blog at laravel serializes model Background  When dispatching an object onto the queue, behind the scenes Laravel is recursively serializing the object and all of its properties into a string representation that is then written to the queue. There it awaits a queue worker to retrieve it from the queue and unserialize it back into a PHP object (Phew!). Problem When complicated objects are serialized, their string representations can be atrociously long, taking up unnecessary resources both on the queue and application servers. Solution Because of this, Laravel offers a trait called SerializesModels which, when added to an object, finds any properties of type Model or Eloquent\Collection during serialization and replaces them with a plain-old-PHP-object (POPO) known as a ModelIdentifier. These identifier objects represent the original properties Model type and ID, or IDs in the case of an Eloquent\Collecti...

Laravel Parallel Testing Is Now Available in laravel8 | Laravelnote

 Parallel Testing | Laravelnote As such we know Laravel and PHP Unit execute your tests sequentially within a single process.  As such laravel check the single process doesn’t use multiple cores so that therefore, your test execution is seriously bottlenecked! we glad to say that Parallel Testing is now available in Laravel. You can use this Laravel version8.25 you may also use to laravel8 built-in test Artisan command to run your cmd to tests simultaneously across multiple processes to use significantly reduce the time required for to run the entire test suite. It is about sure that in laravel8 new on top of Paratest Laravel automatically use to handles creating and migrating a test for database for each parallel process. In The  Laravel8 for testing purpose goodies - such as Storage::fake - are ready for used in Parallel too. Laravel Provide Each all individual laravel8 version use test suite will receive a varying benefits from parallel testing. In The Laravel Tests ar...

Create Your Next Project's Readme in Laravel8 | laravelnote

  The readme.so editor gives you visual cues, starter section templates and includes many standard readme sections you're likely to use. It also has a nice preview to help guide you along the way. Never forget a section for your readme again! Select sections to add to your readme, edit the contents, and drag to rearrange. See a live-updating rendered preview of your markdown, then download your README.md file! Here's an example of starting an API section, which provides helpful formatting. You might need to specify API params: The editor includes both light and dark editor support, and you can download or copy/paste the raw markdown of your readme into version control once you're done. You can learn more about this project and start using it on  readme.so . Also, be sure to check out  readme.so on Product Hunt  and upvote it if you find it useful!