Skip to content

Instantly share code, notes, and snippets.

@Cannonb4ll
Created November 18, 2017 11:47
Show Gist options
  • Save Cannonb4ll/3cb35f181328efab963835ce5b671608 to your computer and use it in GitHub Desktop.
Save Cannonb4ll/3cb35f181328efab963835ce5b671608 to your computer and use it in GitHub Desktop.
A
<?php
function canonical()
{
$route = Route::current();
if (!$route) {
return false;
}
$action = $route->getAction();
if (!$action) {
return false;
}
if (!array_has($action, 'uses') or $action['uses'] instanceof Closure) {
return false;
}
$uses = explode('App\Http\Controllers\\', $action['uses'])[1];
if (!$uses) {
return false;
}
$parameters = $route->parameters();
if ($parameters) {
return action($uses, $parameters);
} else {
return action($uses);
}
}
<?php
namespace App\Traits;
trait Shareable
{
public function getShareUrl($type = 'facebook')
{
if ($type == 'facebook') {
$query = urldecode(http_build_query([
'app_id' => env('FACEBOOK_ID'),
'href' => canonical(),
'display' => 'page',
'title' => $this->title
]));
return 'https://www.facebook.com/dialog/share?' . $query;
}
if ($type == 'twitter') {
$query = urldecode(http_build_query([
'url' => canonical(),
'text' => str_limit($this->title, 100)
]));
return 'https://twitter.com/intent/tweet?' . $query;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment