DEV Community

GabiGlazberg
GabiGlazberg

Posted on

My Laravel setup cheatsheets

  1. router.
  2. controllers.
  3. resources.
  4. admin CMS.

In this article i'm going to show you small pieces of code that I use to scale up my Laravel app before I start to code.
Most of my Laravel projects are structured the same way, website, api and admin system (Content Management System).

router.

  • web.
  • api.
  • admin.

Go to RouteServiceProvider and add this name spaces.

    protected $namespace = 'App\Http\Controllers';
    protected $apiNamespace = 'App\Http\Controllers\Api';
    protected $webNamespace = 'App\Http\Controllers\Web';
    protected $adminNamespace = 'App\Http\Controllers\Admin';

Now add this to the map function.

        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapAdminRoutes();

The mapWebRoutes function will look like this.

    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->as('web.')
            ->namespace($this->webNamespace)
            ->group(base_path('routes/web.php'));
    }

The mapApiRoutes and the mapAdminRoutes functions looks similar to the mapWebRoutes function with ->prefix('') method added to it.
Don't forget to add the necessary files to the routes directory.

controllers

File structure.

|-- app
    |-- Http
        |-- Controllers
            |-- Admin
                |-- BaseController.php
            |-- Api
                |-- BaseController.php
            |-- Web
                |-- BaseController.php
            |-- Controller.php

Api/BaseController.php.

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    /**
     * @OA\Info(title="Project Name Api", version="1.0.0")
     */
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function successResponse($data, int $status = 200)
    {
        return response()->json(
            [
                'data'    => $data
            ], $status
        );
    }

    public function errorResponse($message = 'server error', int $status = 400)
    {
        return response()->json(
            [
                'data'    => $message
            ], $status);
    }
}

Each of my Api Controllers will extend the Api\BaseController.

Resources

File structure.

|-- resources
    |-- assets
        |-- admin
        |-- web
    |-- lang
        |-- en
        |-- he
    |-- views
        |-- admin
        |-- web

From now on i'm going to focus on the admin system, but feel free to take some of the concepts to the web.

Admin

When I build the admin system i'm focusing on:

  • admin plugin. There are many in the market it costs around 10 bucks.
  • webpack.
  • rtl ltr css.
  • views.
  • sidebar.
  • toastr.
  • all the laravel basic: migration, model, auth, guard, middleware etc. (there is many usefull info about all of them on the web).
|-- resources
    |-- assets
        |-- admin
            |-- images
            |-- js
            |-- sass
                |-- components
                |-- vendor
                    |-- bi-app
                |-- app.scss
                |-- main-ltr.scss
                |-- main-rtl.scss
            |-- someAdminSkinThatYouNeedToBuy
            |-- webpack.mix.js
            |-- package.json
        |-- web
    |-- lang
    |-- views
        |-- admin
            |-- auth
            |-- layouts
            |-- pages
            |-- partials
        |-- web

resources/assets/admin/webpack.mix.js.

var mix = require('laravel-mix');

var adminPublicPath = '../../../public/assets/admin/';

mix
    .sass('sass/main-ltr.scss', adminPublicPath + 'css/app.css')
    .sass('sass/main-rtl.scss', adminPublicPath + 'css/app-rtl.css')

    .combine([
    'someAdminSkinThatYouBought/global/plugins/bootstrap/css/bootstrap-rtl.css',
    'someAdminSkinThatYouBought/global/plugins/font-awesome/css/font-awesome.css',
    'someAdminSkinThatYouBought/global/plugins/simple-line-icons/simple-line-icons.css',
    'someAdminSkinThatYouBought/global/plugins/bootstrap-switch/css/bootstrap-switch.css',
    ...

The rest of the file is used to bundle plugins, js, images, css etc. Most of them are bootstrap and jquery plugins.

css ltr rtl

resources/assets/admin/sass/_app.scss.

@import "variables";

// Bootstrap
@import './override-bootstrap';
@import 'custom';

//Component
@import 'component/login';
...

resources/assets/admin/sass/main-ltr.scss.

@import 'vendors/bi-app/bi-app-ltr';
@import 'app';

resources/assets/admin/sass/main-rtl.scss:

@import 'vendors/bi-app/bi-app-rtl';
@import 'app';

resources/views/admin/layouts/app.blade.php.

    <link href="{{ url((config('app.direction') == 'rtl') ? 'assets/admin/css/app-rtl.css' : 'assets/admin/css/app.css') }}?v={{ env('version', time()) }}" rel="stylesheet" type="text/css" />

Don't forget to add direction to the config/app.

side-bar

config/admin-sidebar.php.

<?php

return [

    'dashboard' => [
        'route'   => 'admin.dashboard',
        'icon'    => 'fa fa-dashboard',
        'display' => 'Dashboard',
    ],
]

app/Http/Middleware/AdminSidebar.php.

    public function handle($request, Closure $next)
    {
        $adminSideBar = config('admin-side-bar');

        View::share('adminSideBar', $adminSideBar);

        return $next($request);
    }

routes/admin.php

Route::group([
    'middleware' => [
        'auth_admin', 'admin_side_bar'
    ],

], function () {
...

And finally the sidebar is in use. resources/views/admin/partials/side-bar.blade.php.

        <ul class="nav nav-list">
            @foreach($adminSideBar as $sideBarItem)
                @if(isset($sideBarItem['items']))
                <li>
                    <a class="dashboard" href="javascript:;">
                        <i class="main-icon {{ array_get($sideBarItem, 'icon') }}"></i>
                        <span>{{ array_get($sideBarItem, 'display') }}</span>
                    </a>

                    <ul>
                        @foreach($sideBarItem['items'] as $adminSideBar => $sideBarItemChild)
                            <li>
                                <a href="{{ route(array_get($sideBarItemChild, 'route')) }}">{{ array_get($sideBarItemChild, 'display') }}</a>
                            </li>
                        @endforeach
                    </ul>
                </li>
                @else
                    <li>
                        <a class="dashboard" href="{{ route(array_get($sideBarItem, 'route')) }}">
                            <i class="main-icon {{ array_get($sideBarItem, 'icon') }}"></i>
                            <span>{{ array_get($sideBarItem, 'display') }}</span>
                        </a>
                    </li>
                @endif
            @endforeach
        </ul>
  • toastr resources/views/admin/layouts/app.blade.php.
  function showToast(message, title, type) {
        if(!type)
            type = 'success';

        if(title)
            toastr[type](message, title);
        else
            toastr[type](message);
    }

    $(document).ready(function() {
        @if(session('toastr'))
            showToast('{{ session('toastr')['message'] }}',
                {!! isset(session('toastr')['title']) ? "'".session('toastr')['title']."'" : 'false' !!},
                {!!isset(session('toastr')['type']) ? "'".session('toastr')['type']."'" : 'false'!!});
        @endif
    });

app/Http/Support/helper.php.

if (! function_exists('toastr')) {
    function toastr($data) {
        session()->flash('toastr' ,$data);
    }
}

Don't forget to include this in the composer.json.

"files": [
            "app/Support/helpers.php"
        ]

An example of toastr function.

toastr([
    'type'      => 'success',
    'title'     => 'you did a great job',
    'message'   => 'thank you come again'
]);

references

Top comments (0)