Search code examples
phplaravellaravel-5laravel-authorization

Auth Routes/Views in Laravel Package?


I've been developing a custom package for Laravel 5.2 and I am at the point now where I am implementing Authentication.

I tried adding an Auth folder to my resources/views directory as well as the Controllers needed within my package directory with proper namespaces. It hits the the controllers but does not see the view as it returns an error saying [auth.login] not found.

I tried adding the property overides to the AuthController as such:

protected $loginView = 'packagename::auth.login';
protected $registerView = 'packagename::register.form';
protected $linkRequestView = 'packagename::link.request.view';
protected $resetView = 'packagename::reset.view';

However, it still is trying to hit the views located in the main Laravel app directory, and not the package.

What is the best practice/proper way to setup Auth views,routes etc within a Laravel package so that it can have it's own admin area?


Solution

  • There are many of strategies and it depends of what (and how big) the application is. I can give you an example of naming.

    Views: {package-name}::{area}.{module}-{submodule?}.{action}
    Routes: {package-name}.{area}.{module}-{submodule?}.{action}
    Config: {package-name}.{area}.{rest-of-config-key}
    

    Let me explain:

    1. {package-name} - you should always put in front that you will know everytime what package does view/route/config is from.

    2. {area} - for exampla can be: admin and user and front or restricted and open. It will describe the level of the access of the user (for example front doesn't need logged user). This part is good especially for creating routes groups and it filters (but also for harmonize every element of the system).

    3. {module} and {submodule} - will be telling you what kind of business logic you are in.

    4. {action} - is just a name of controller's action

    So here you have same examples of the above:

    route('mypostpackage.admin.post.index')
    route('mypostpackage.admin.post-comment.edit', [$id])
    

    view('mypostpackage::admin.post.show') // in package: views/admin/post/index.blade.php
    view('authpackage::restricted.user-address.edit')
    

    This also can be used in designing struture of the files (views and controllers and any other app logic that you what to use in this convention).

    Remember that this is just a base for design so you can redisign it whatever you like.