Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KFoobar committed Feb 6, 2020
0 parents commit a8515cf
Show file tree
Hide file tree
Showing 9 changed files with 655 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.phpintel/
vendor/
composer.lock
.DS_Store
16 changes: 16 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Copyright 2019 David Villa ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
195 changes: 195 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# Laravel package to manage meta and title tags

This package makes it easier to work with HTML meta and title tags in Laravel 5 and Laravel 6.

The package can be used in any controller with the facade or in any blade file with the facade or the blade directives.

The package supports most meta tags. It also supports the `<title>` tag and the `<link>` tag for canonical since they are commonly used.

## Installation

Begin by installing the package with Composer from your command line:

```
$ composer require kfoobar/laravel-meta
```

### Laravel 5.5 or later

If you use auto-discovery, you don't need to do anything more to enable the package.

### Laravel 5.4 or earlier

You need to register the service provider with your Laravel application in `config/app.php`:

```php
'providers' => [
'...',
KFoobar\LaravelMeta\MetaServiceProvider::class,
];
```

Also add the facade to the same config file:

```php
'aliases' => [
'...',
'Meta' => KFoobar\LaravelMeta\Facades\Meta::class,
];
```

## How to use

### How to use the facade

If you want to set your tags in a controller, you'll need to add the namespace for the `Meta` facade.

```php
<?php

namespace App\Http\Controllers\Posts;

use App\Http\Controllers\Controller;
use KFoobar\LaravelMeta\Facades\Meta;

class PostController extends Controller
{
...
```

#### Set values

You can set the tag values in your controller with the `set` method.

```php
public function index()
{
Meta::set('title', 'Your new page title');
Meta::set('description', 'Your new meta description');
// or...
Meta::setTitle('Your new page title');
Meta::setDescription('Your new meta description');
}
```

#### Get values

And if you need to, you can also get the values with the `get` method.

```php
public function index()
{
Meta::get('title', 'Default title if none is set');
Meta::get('description', 'Default description if none is set');
// or...
Meta::getTitle('Default title if none is set');
Meta::getDescription('Default description if none is set');
}
```

#### Get the full html tag

I you want the full html tag as a string, use the `tag` method.

```php
$titleTag = Meta::tag('title');
$titleTag = Meta::tag('title', 'Default title if none is set');
```

#### Check if value is set

If you need the check if any value is set, use the `has` method.

```php
if (!Meta::has('title')) {
Meta::set('title', 'This is a fallback title');
}
```

### How to use the blade directives

You can use the facade directly in you blade files, but there are a few blade directives that will make your blade files look a bit cleaner.

#### Set values

By using the `@setMeta` directive, you can set the values of your title and meta tags in any blade file.

```php
@setMeta('title', 'Your new page title');
@setMeta('description', 'Your new meta description');
```

#### Get values

Use the `@getMeta` directive when you want to get the value of your title and meta tags.

```php
@getMeta('title')
@getMeta('title', 'Default title if none is set')

<title>
    @getMeta('title', 'Default title if none is set')
</title>
```

#### Get the full html tag

You can use the `@getTag` directive when you want to print the full html tag.

```php
@getTag('title')
@getTag('title', 'Default title if none is set')

@getTag('description')
@getTag('description', 'Default description if none is set')
```

#### Check if value is set

When you need the check if any value is set, use the `@hasMeta` directive.

```php
@hasMeta('author')
<meta name="author" content="@meta('author')">
@endhasMeta
```

You can also use `@endif` instead of `@endhasMeta` if you prefer.

## Example

This is our blade file:

```php
@getTag('description')
@getTag('keywords')
@getTag('robots', 'index, follow')
@getTag('csrf-token')

@getTag('title', 'Default title if none is set')

@hasMeta('canonical')
    @getTag('canonical')
@endhasMeta
```

This will be the output:

```php
<meta name="description" content="Lorem ipsum dolor...">
<meta name="keywords" content="lorem, ipsum, dolor">
<meta name="robots" content="index, follow">
<meta name="csrf-token" content="4ipX8A07Awf60ZUBPBfy...">

<title>Default title if none is set</title>

<link rel="canonical" href="https://example.com/this-is-a-slug">
```

## Contributing

Contributions are welcome!

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "kfoobar/laravel-meta",
"description": "Laravel package to manage title and meta tags",
"license": "MIT",
"authors": [
{
"name": "KFoobar",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=7.0",
"illuminate/support": "^5.5|^6"
},
"autoload": {
"psr-4": {
"KFoobar\\LaravelMeta\\": "src/KFoobar/LaravelMeta/"
}
},
"extra": {
"laravel": {
"providers": [
"KFoobar\\LaravelMeta\\MetaServiceProvider"
],
"aliases": {
"Meta": "KFoobar\\LaravelMeta\\Facades\\Meta"
}
}
}
}
13 changes: 13 additions & 0 deletions src/KFoobar/LaravelMeta/Facades/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace KFoobar\LaravelMeta\Facades;

use Illuminate\Support\Facades\Facade;

class Meta extends Facade
{
protected static function getFacadeAccessor()
{
return 'meta';
}
}
Loading

0 comments on commit a8515cf

Please sign in to comment.