How to extend Laravel 5 auth properly?

Is porting a complex non-MVC Coldfusion site to PHP framework Laravel feasible?

  • I am tasked with porting a site to a new environment that was built many years ago, from scratch, without losing any functionality. It's a small ecommerce site, but due to the nature of that particular business, there are lots of discounting and pricing structures that I don't imagine to be easily recreated in a framework. However, I have been looking over info on PHP frameworks, and Laravel looks great so I want to give it a shot. This will be my first use of a framework, so I am looking for advice on whether or not something that I know will need to be very custom on the backend would be a good fit for a framework. The SQL is going to be more advanced than the examples that I have seen, not to say it can't be done, I just haven't seen any examples of complex SQL. Also, I will be doing a SQL- or Session-based shopping cart as well as user auth and a CMS. Would Laravel be a good fit for this project and as a first project with a framework? Thanks and sorry for being so long-winded.

  • Answer:

    I've used Laravel for several small projects and have found it a delight to work with. I'd highly recommend using it for most web applications, although there are certainly cases in which another framework (or language) is better suited. Currently, I'm building a larger scale platform with Laravel and am very pleased with its flexibility to solve more complex problems. As far as your concerns go, Laravel should handle everything you want to do, and its community is very helpful in case you can't find the answer by Googling or Stackoverflow. Your pricing structures and discounts can be defined in your models and easily made available to the managing users for updating through the CMS. While there aren't many complex SQL examples in the Laravel docs, there are other resources available. Check out the tutorials on Net Tuts: https://tutsplus.com/course/laravel-essentials/ https://tutsplus.com/course/hands-on-building-a-practical-web-application-with-laravel/ https://tutsplus.com/course/rails-vs-laravel/ (this covers slightly more complicated SQL). These tutorials are on their premium site, which you have to pay for. But the $20 or so is by far the best investment you can make if you are seriously considering working with this framework (or many others). Laravel's Eloquent ORM is very nice to work with, and you can do very complex commands through its methods. Defining relationships (one-to-one, one-to-many, many-to-many, polymorphic, etc) is well-documented, but you can easily blend more SQL-like code with Eloquent (or Fluent, its other database interface). Alternatively, you can write plain SQL for your application and execute it directly, but you'll likely find that working with Eloquent is so much better. Here's an example from a previous project I worked on (this is a co-workers code, I'll give him credit if he wants it :P). It shows how you can write blended SQL and Fluent code that is easy to understand and is nearly as visually descriptive as SQL. This project didn't fully take advantage of Laravels relationship modeling, so unfortunately I don't have the equivalent Eloquent for this: <?php public static function get_all_gallery_images($filters = array()) { $query = DB::table(static::$table) ->join('images', 'gallery_images.image_id', '=', 'images.id') ->join('galleries', 'gallery_images.gallery_id', '=', 'galleries.id') ->join('events', 'galleries.event_id', '=', 'events.id'); if (array_key_exists('id', $filters)) { $query->where('gallery_images.gallery_id', '=', $filters['id']); } elseif (array_key_exists('slug', $filters)) { $query ->where('events.slug', '=', $filters['slug']); } return $query->get(array( 'images.filename AS filename', 'events.title AS title' )); return false; } Laravel handles persistence in a very elegant way. Database connections are set up in configuration files (very standard in frameworks), and switching between development, staging and production environments is a breeze. Whether you use Session or Database persistence for your shopping cart, you'll find that it is very straightforward to do either. The docs do a great job explaining how to use Sessions, so I won't go on about it here, just take a look at the official documentation: http://laravel.com/docs/session/usage For your Authentication support, Laravel has a flexible Auth driver built in that you can modify to meet a wide range of needs. If you have a username/email & password log-in, its practically set up for you. Just define a User model and you can access Laravel's Auth class to do things like: <?php if(Auth::check()) { return "You're logged in."; } else { return Redirect::to_route("login"); } // or getting user information: $user_id = Auth::user()->id; Additionally, with Laravel's wonderful routing implementation, you could group a collection of routes for your CMS. For instance, you can define a route group so that everything behind the /admin/ URL segment requires authentication. There's plenty more to learn, but with its exceptional documentation, available resources and great community, you would be making a great choice to jump in with Laravel.

Steve Manuel at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.