Giter Site home page Giter Site logo

laravel-widgets's Introduction

Build Status

#Widgets for Laravel

This packages provides widget functionality to boost your Laravel views. Includes asynchronous mode and generator.

Installation

composer require arrilot/laravel-widgets

Note: for Laravel 4 use composer require arrilot/laravel-widgets ~1.0

Then, register a service provider in your app.php config file

<?php

'providers' => [
    ...
    'Arrilot\Widgets\ServiceProvider',
],
?>

Finally, add the facades here too.

<?php

'aliases' => [
    ...
    'Widget'       => 'Arrilot\Widgets\Facade',
    'AsyncWidget'  => 'Arrilot\Widgets\AsyncFacade',
],
?>

Usage

Lets consider we want to make a list of recent news and reuse it in several views.

First of all we can create a Widget class using the artisan command provided by the package.

php artisan make:widget RecentNews

Now the folowing widget skeleton is created in your app/Widgets directory:

<?php namespace App\Widgets;

use Arrilot\Widgets\AbstractWidget;

class RecentNews extends AbstractWidget {

    /**
    * You can treat this method just like a controller action.
    * Return a view or anything you want to display
    */
	public function run()
	{

	}
}

As soon as domain logic is implemented inside the run() method, the widget can be included to a view in several ways:

{!! Widget::run('recentNews') !!}

or

{!! Widget::recentNews() !!}

or even

@widget('recentNews')

That's all!

Configuration

Namespaces configuration

By default package tries to find your widget in the App\Widgets namespace.

You can override this by publishing package config and setting default_namespace property.

Although using the default namespace is very convenient and keeps you from doing unnecessary actions, in some situations you may wish more flexibility. For example, if you've got dozens of widgets it makes sense to group them in namespaced folders.

You actually have several ways to call those widgets:

  1. You can register widget in package config like that:
    'custom_namespaces_for_specific_widgets' => [
        'recentNews' => 'App\Widgets\News'
        ....
    ]

And then call it normally

{!! Widget::recentNews($config) !!}
{!! Widget::run('recentNews', $config) !!}
@widget('recentNews', $config)
  1. You can pass full name to the run method
{!! Widget::run('News\RecentNews', $config) !!}
@widget('News\RecentNews', $config)
  1. You can also use dot notation if you like it
{!! Widget::run('news.recentNews', $config) !!}
@widget('news.recentNews', $config)

Widget configuration

Using config array

Let's carry on with the "recent news" example.

Imagine that we usually need to show 5 news, but in some views we need to show 10. This can be easily achieved like that:

class RecentNews extends AbstractWidget {
    ...
    protected $count = 5;
    ...
}

...
{!! Widget::recentNews() !!}
...
{!! Widget::recentNews(['count' => 10]) !!}

['count' => 10] is a config array. Notice that you don't need to map a config array and class properties in constructor. It's done automatically behind the scene

Using additional parameters

You can also choose to pass additional parameters to the run() method directly if you like it.

{!! Widget::recentNews([], 'date', 'asc') !!}
{!! Widget::recentNews(['count' => 10], 'date', 'asc') !!}
{!! Widget::run('recentNews', ['count' => 10], 'date', 'asc') !!}
@widget('recentNews', ['count' => 10], 'date', 'asc')
...
public function run($sort_by, $sort_order) { }
...

run() method is resolved via Laravel service container so method injection is available here too.

Asynchronous widgets

In some situations it can be very beneficial to load widget content with AJAX.

Fortunately this can be achieved very easily!

All you need to do is to make sure you have jquery loaded for ajax calls and change Widget:: => AsyncWidget::, @widget => @async-widget

If asynchronous mode causes you any problems you can always change it back.

laravel-widgets's People

Contributors

arrilot avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.