Block IP address from accessing Laravel application

In many cases we need to block some particular IP address from accessing our website content or application. We can restrict IP in Laravel through a simple piece of code.

How to block or restrict any IP address from accessing your Laravel application ? lets see. We can create a Middleware to check the blocked IP address and redirect to the page we want.

  1. Create a Middleware “RestrictIpMiddleware.php”.
  2. Paste the code inside handle function.
  3. Register the middleware in kernel function.
  4. Add the registered middleware in routes in order to restrict the access.

Creating a Middleware

php artisan make:middleware RestrictIpMiddleware

The Code

Paste the below code in the handle function of the middleware


 $restricted_ip = "Comma seperated IP address which is to be restricted"; 
 $ipsDeny = explode(',',preg_replace('/\s+/', '', $restricted_ip));
 if(count($ipsDeny) >= 1 )
 {
    if(in_array(request()->ip(), $ipsDeny))
    {
        \Log::warning("Unauthorized access, IP address was => ".request()->ip);
         return response()->json(['Unauthorized!'],400);
    }
 }
 return $next($request);

Install Laravel Valet in Ubuntu

The official Laravel Valet development environment is great if you are an Apple user and thanks to Carlos Priego you can now enjoy the same benefits you are an Ubuntu user.

Lets see how to install Valet in Ubuntu 16.04 LTS.
Installation is really simple. First, make sure you are running Ubuntu >= 15.04 with PHP >= 5.6.
1. Now you’ll need the following dependencies and packages:

sudo apt-get install libnss3-tools jq xsel

2. PHP Packages (replace the * with your PHP version):

php*-cli php*-common php*-curl php*-json php*-mbstring php*-mcrypt php*-opcache php*-readline php*-xml php*-zip

3. The final step

composer global require cpriego/valet-ubuntu
valet install

That’s all. Cheeeers 😉 You have Valet in your machine. Enjoy 🙂

Top 5 PHP Frameworks in 2017 that you should try

Here we are gonna discuss the top 5 PHP frameworks in 2017. According to the TIOBE Index of programming popularity, PHP ranks in the 10th position. As a language that was originally designed for web development, but due to its sudden rise in popularity in the past few years, many programming enthusiasts developed several frameworks for developing non-web applications as well. PHP is used in pretty much everywhere in the web domain as the main server side language, whether it’s the blogging platforms, like WordPress, Tumbler, etc. or the websites of giant organizations, like Google and Facebook. One can easily see or inspect the PHP code in majority of the sites they encounter. Many newbie PHP developers are tempted to learn frameworks, as normally the frameworks provide all the packages needed for building the applications in a particular language. But, when they search on the PHP frameworks, they become indecisive about the framework that they should use.

Thus, in this article, I am going to highlight top 5 PHP Frameworks In 2017 that you should definitely try. Although , PHP can be used for non-web applications as well, I will only highlight web development frameworks, as for this domain, PHP is generally considered a standard. Let’s get started!

#5 Silex

It is based on Symfony2 components, meaning if you’ve used the Symfony in the past that you might feel at home using Silex. It can be even considered the alternative user interface for the similar components, as long as you understand the original components. The cool thing about Silex is that it has very little to no boilerplate code to configure the routes. As Silex uses the same components of Symfony, the migration between the frameworks is extremely easy, so if you’ve already started developing PHP applications through Symfony, you might need to look into Silex. The model and view parts of the Silex are handled by third-party libraries, such as, Twig and Doctrine, so it’s pretty much a controller framework that ease the use of using the libraries in PHP. I recommend this framework if you are looking for a light-weight version of Symfony with less bundles.

#4 CodeIgniter

This framework was disliked by PHP community when it arrived for the first time in 2006 (using PHP 4), and people thought that writing PHP apps through CodeIgniter would be cumbersome, especially when EllisLab stopped supporting it for free. Thankfully, since its recent releases as open-source PHP framework, it has become an extremely matured scripting language framework for the fast and bleeding edge PHP applications. Its recent versions recommend PHP 5.6 to operate, although its minimum requirements is PHP 5.3.7. It has a very small foot-print, and built specifically for developers who like to do minimal coding for the optimal results. It provides enough capabilities to improve your productivity and efficiency, while providing the third-party add-ons or plugins for additional functionality.This framework is recommended if you are just getting started with PHP.

#3 Symfony

Symfony is a PHP full-stack web framework. The main motivation for writing the framework was to maximize the speed and flexibility. It is open-source and community-driven project, meaning not only you can edit or read the source code, but also discuss with other enthusiasts about the future development. According to its website: Symfony is a set of PHP Components, a Web Application framework, a Philosophy, and a Community — all working together in harmony. Symfony is designed with Symfony components (that are used in a few other PHP frameworks like Laravel) as well as with the help of a huge community. It is one the most minimalist PHP framework. If you ever worked with Ruby on Rails, then you will realize that its structure has a lot of similarities with it, so you get the power of Rails without having to learn Ruby. It has the yaml interface for the configuration, and has several security components for making sure your code runs as you expect without any threat.

#2 Zend Framework

As compared to other PHP frameworks, this framework is quite heavy weight and has a steep learning curve, especially if you are just starting the PHP development. Through it, you can build maintainable, testable and secure web apps with little to no worry about building the libraries or packages on your own. It is very modular, and every class has its own place, such that you don’t even need to load a class to run a plain code, though certain classes might be loaded in the background, but you don’t need to worry about it.

It is extremely lightweight, and you only require to download few MBs. It is a huge improvement over Zend Framework, which required several lines of code to perform few operations. This framework is highly recommended to those who already understand the PHP language, and looking for the pragmatic solution to write seamless code without worrying about the repetition.

#1 Laravel

This is my favorite open-source PHP framework, meaning you can read or edit the whole source code available on Github as per your needs (although you might not need to do so, as the framework itself is extremely flexible). It has the best modular packaging system. Laravel uses several external packages for its functionalities, but thankfully, the framework is provided with Composer which is a modern dependency manager, installer, and class autoloader, which makes it extremely easy for you to define classes or create projects wherever you want, as long as it is loaded by Composer then you don’t have to worry about anything. This means that you can create a new Laravel project right away without worrying about cloning the packages yourself. This feature makes it extremely compelling to the new users who want to learn the PHP using Laravel, as they are tired of downloading new packages whenever they pick some framework for learning PHP.

The best thing about this framework is that it is specifically designed for artisans, meaning they put huge emphasis on making the code elegant and easy-to-read. It is provided with the command-line called Artisan (note the similar name for the same reason) which makes it extremely easy to perform several task through simple commands. It uses Eloquent as its ORM. For the debugging, it uses Whoops library, which takes care of all the errors in the code. It also supports Database Migration through migrations and seeds, meaning you can migrate your database to the other hard-drive, as long as you have stored it on the migrations and seeds. It has been in a great rise, especially since the advent of Laravel 5. I would highly recommend this framework to anyone who is starting to learn PHP.

Conclusion

In this article, I have highlighted top 5 PHP Frameworks that I strongly believe will be sufficient for your needs. Granted, if you require the PHP skills for the jobs, learning these frameworks might be sufficient, as not only it builds your coding experience, but also the techniques that would be beneficial in a long-run.

If you have any thoughts on this, then you can post in the comment section below !