Nasrul Hazim Bin Mohamad

Laravel: Organising Your Route Files

Jul
22

Assalamualaikum / Hi,

Have you ever having hundred or probably thousand line of codes in a single routes/web.php ?

So, my solution is quite simple.

First thing, let’s look at this code snippet:

collect(glob(base_path('/routes/web/*.php')))
    ->each(function ($path) {
        require $path;
    });

So, it’s quite straightforward - the code above will simply require all files in the routes/web directory.

Means that, you can have infinite number of files in the directory.

All you need to do now, is to use the above code snippet for your web and api routes and please make sure to create web and api directory in routes directory.

Now you can move existing route file into respective folders and add as much files as you want in the directory.

Do checkout my route setup in Laravel Boilerplate.

Now, we have organised routes and no more hundred or thousand line of codes in single file!

Yeah!

Laravel: CRUD Breadcrumb

Jul
10

Assalamualaikum & Hi,

Today, I just want to share a little tip on managing your breadcrumb when using Laravel Breadcrumbs.

It is common to have following breadcrumb setup:


Main Menu > Module
Main Menu > Module > Create
Main Menu > Module > Edit
Main Menu > Module > Details

And it is kind of messy if you have lots of module to setup the breadcrumbs.

So, I do some digging on Laravel Breadcrumbs codes and found out the main BreadcrubManager class do have use Macroable trait!.

So the solution for the above use case is to extend the functionality of the breadcrumb using macro! Yeah!

Keep it short, grab the following codes, and call it in your service provider \App\Macros\Routing\Breadcrumb::registerMacros() (my favourite approach in registering macros!)

The usage will be like the following:

Now you can attach a lot more breadcrumbs!

Bonus

Add the following code snippet at the end of your routes/breadcrumbs.php to include multiple breadcrumb files in routes/breadcrumbs/ directory – of course you need to create the directory first.

Thanks for reading!

Oracle: Installing PHP7.2 Oracle OCI on MacOSX

Jul
09

Assalamualaikum,

Just a little bit of sharing on how to install PHP7.2 Oracle OCI on MacOSX. Original gist by gido, But I have update with latest PHP7.2.

Nothing much changed, except on handling some error / misconfigure (probably due to my machine got a little weird setup previously).

Anyway, here the gist for the setup.

Laravel: Display Progress Bar in Seeder

Jun
18

Assalamualaikum / Hi,

Recent projects development require me to have numbers of seeders – this include some dummy data.

Each time seeding new / old data, it’s kinda boring to wait it’s done.

So I just need to know, how much the progress of the seeding. Following is the trait I’ve created to generate the progress bar.

Sample output:

Hope this small snippet benefit anyone in need displaying the progress of the seeding.

Happy coding guys!

Laravel: Import Templates into Laravel

Apr
27

Assalamualaikum,

We know, there’s millions of web templates available out there – the clean look, corporate look, modern, minimalist. You name it, either it is free or paid version, usually it come in HTML, JS and CSS format. It’s hard to find template that is ready with Laravel.

Previously, here are some of the approaches I did, to get the template into Laravel application.

  1. Download the template, copy the assets, organise them in public directory. That’s ok, but I need a way to keep everything in resources/assets.
  2. Download the template, mimic / copy the important part of the template – CSS / JavaScript – place it in resources/assets and start writing codes for front-end assets, then compile using Laravel Mix.
  3. Get the list of dependencies used for the theme, manually install using yarn add..., then create scripts – CSS / JavaScripts – in resources/assets, then compile using Laravel Mix.

All these some of my previous approaches in importing templates into Laravel application.

Yesterday, I have a chance experimenting on new method. Here the workflow:

  1. Clone / Download the template into resources/assets/templates/
  2. In terminal, go into resources/assets/templates/the-template/, run npm install && npm run build – depending on your template selection, sometimes it is required to compile the template, but there is templates that did not require you to compile.
  3. Then in webpack.mix.js, copy all assets generated in resources/assets/templates/the-template/ to public/the-template.

Then you are done! You can start use the asset() helper like asset('the-template/js/vendor.js'), and so on in your layouts.

Following an example of the templates I have compile and export to public folders – but I did not implement in the new layout, as it require a lot of efforts to create components and use all the classes from the template.

Template Into Laravel

Hope my explanation will make you guys clear, how to import templates into Laravel projects – 3 main steps: git clone, build(optional), copy compiled assets with mix to public directory.

Thanks!

Laravel: Reset Password From an API

Apr
27

Assalamualaikum,

Today, will share about how to reset your account in Laravel application from an API.

This is assumed you already setup JWT Auth in your Laravel app.

Create a new API controller – php artisan make:controller Api/Auth/ForgotPasswordController and copy paste the following code, and add the forgot password route in routes/api.php.

So, basically what I did was to use Illuminate\Foundation\Auth\SendsPasswordResetEmails trait, which being used in the default Laravel Forgot Password Controller.

I just copy codes from sendResetLinkEmail method, and make changes on how the response we should receive from an API.

Then you are done! Easy right? 🙂

To test, open up your email, you just need to pass email address – just like we did in Laravel Auth Scaffold, then you are done.

The email owner will receive reset password link, and the owner open up the email, click on the link, go reset as usual, as we did on web app.

That’s all, thanks!

Laravel: HasDatatable Trait

Apr
26

Assalamualaikum,

Just a simple setup on datatable – to have a trait that can be use for other models.

Assumed that you already install Yajra Datatable, we going to setup the API end point, simplify the setup of datatable.

Here the trait:

You can use the protected $datatable property to select which fields to display in your datatable.

The usage:

The API Endpoint:

Then you are done!

You may overwrite the datatable scope in your model if you need other implementation for the scope.

This is just one neat simple setup, reusable to your other models.

Hope this can help speed setup your datatable API Endpoint.

Thanks for reading!

p/s: You may see this setup in Cleanique Coders Boilerplate.app.

Laravel: Observer Setup Enhancement

Apr
24

Assalamualaikum,

It’s been awhile since my last blog post. Today, just want to share something simple, an enhancement of LARAVEL: CREATE HASHED ID FOR A RESOURCE USING OBSERVER. Going to refactor a bit.

First, let’s modify the app/Observers/Kernel.php, to be like the following:

Next, add new config file for our observer – config/observers.php:

And that’s it!

Very simple and straightforward setup of Laravel Observer, much cleaner than before.

Should I make this as a package? Do let me know if you guys need this as a package. 🙂

Thanks

Laravel: Create Telegram Bot Application without Botman Studio

Mar
21

Installation

We are using Botman package, and Botman Telegram Driver.

Configure .env

Use Bot Father to create new bot and get the API Token for the bot, store it in .env.

TELEGRAM_TOKEN="your-bot-api-token"

Telegram End Point

Create a route & controller for your Telegram Bot to use after this (webhook).

In TelegramController, add the following:

Now you are done preparing for next part.

Webhook

For development purpose, you can use valet share to have a live server. For Windows / Linux, you can install ngrok.io to have a live server.

valet share is actually running a ngrok.

Run command valet share and and then register our app with Telegram using php artisan botman:telegram:register command.

CSRF Token

In App\Http\MiddlewareVerifyCsrfToken, disable CSRF token for /telegram route.

One you are done setting up the webhook, open up your Telegram, start talk with your bot, /hi. You should received response from your bot.

Laravel: Minify HTML

Feb
25

Assalamualiakum / Hi,

Just a quick blog post this time.

As part of optimisation, it’s important to minify your JavaScript & CSS – this can be done with Laravel Mix.

But how about HTML?

Seen this?

Ugly HTML Codes

So that’s how it’s look like our generated HTML codes.

And how we going to change into this?

Minified HTML Codes

A simple middleware will do to minify our HTML.

Then register the middleware and you are good to go!

Just grab this the Laravel Page Speed, then you’re done!

Thanks,
Nasrul