Facade not found by AliasLoader in Laravel - laravel

I added a custom facade to my 'config/app.php' in my laravel project under 'aliases'
'GeoLocation' => '\App\Facades\GeoLocation',
The folder of the custom class is in 'app/Facades/GeoLocation.php' and the service provider in 'app/Providers/GeoLocationServiceProvider.php'
How do I need to state the correct alias in the app.php to be able to load the Facade correctly? The error message is:
ErrorException in AliasLoader.php line 63:
Class 'Facades\GeoLocation' not found
This is my facade:
<?php namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class GeoLocation extends Facade {
protected static function getFacadeAccessor() { return 'geolocation'; }
}
Could it be that the return value of my service provider is incorrect?
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class GeoLocationServiceProvider extends ServiceProvider {
public function register() {
\App::bind('geolocation', function()
{
return new GeoLocation;
});
}
}
For a test I created another service provider called custom function:
<?php namespace App\Helpers;
class CustomFunction {
//Generate random float between -1 and 1
function f_rand($min = 0, $max = 1, $mul = 1000000) {
if ($min>$max) return false;
return mt_rand($min*$mul, $max*$mul) / $mul;
}
function test() {
echo "OK";
}
}
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class CustomFunctionServiceProvider extends ServiceProvider {
public function register() {
\App::bind('customfunctions', function()
{
return new CustomFunction;
});
}
}

the simplest way is to change the namespace to the file app/Facades/GeoLocation.php' to App\Facades;
then update aliase registration to
'GeoLocation' => 'App\Facades\GeoLocation',

Related

Laravel and Local Tunnel integration

My idea was to use Local Tunnel's subdomain feature to expose callback URI in a more convenient way. However, I believe that I could've achieved the same results in a simper way.
This is the solution with Laravel Valet:
In package.json I've added a script called shared
"scripts": {
...
"share": "lt --port 80 --subdomain blog --local-host blog.test"
}
In AppServiceProvider.php I've extended the UrlGenerator to avoid redirecting back to http://blog.test
<?php
namespace App\Providers;
use App\Services\LocalTunnelUrlGenerator;
use Blade;
use Illuminate\Http\Resources\Json\Resource;
use Illuminate\Routing\Router;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
(...)
}
public function register()
{
$this->enableLocalTunnel();
}
private function enableLocalTunnel()
{
if (!app()->environment('local') || !config('app.use_local_tunnel')) {
return;
}
$this->app->extend('url', function (UrlGenerator $defaultGenerator) {
/** #var Router $router */
$router = $this->app['router'];
$routes = $router->getRoutes();
return new LocalTunnelUrlGenerator($routes, $defaultGenerator->getRequest());
});
}
}
This is the the LocalTunnelUrlGenerator.php:
<?php
namespace App\Services;
use Illuminate\Http\Request;
use Illuminate\Routing\RouteCollection;
use Illuminate\Routing\UrlGenerator;
class LocalTunnelUrlGenerator extends UrlGenerator
{
public function __construct(RouteCollection $routes, Request $request)
{
parent::__construct($routes, $request);
}
public function formatRoot($scheme, $root = null)
{
return "https://blog.localtunnel.me";
}
}
Why all that? Because whenever the application call the redirect() method, we are sent back to http://blog.test.
Do I really need to extend the UrlGenerator to make it work?

Target [App\Http\Controllers\IndexController] is not instantiable. in laravel

I have cloned project from github and installed in my local system everything is working fine.
And i created controller through command, the controller is created but when i try to use controller function the error shows me like below.
BindingResolutionException
Target [App\Http\Controllers\SomeController] is not instantiable.
in Container.php (line 895)
I tried to solve this problem by running command below:
php artisan cache:clear
php artisan clear-compiled
composer dump-autoload
php artisan optimize
php artisan config:clear
But i still got same error. Kindly help me to resolve this issue.
My controller is:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SomeController extends Controller
{
public function getIndex() {
echo "string";
}
}
AppServiceProvider.php :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
Controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Carbon\Carbon;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected function __construct() {
$variable2 = "I am Data 2";
View::share ( 'variable2', $variable2 );
}
protected function create_permission($role_type_id,$module_id)
{
$CheckCreatePermission = \DB::table('role_type_access')->where(['role_type_id'=> $role_type_id,'module_id'=>$module_id])->select('create')->get();
if(!empty($CheckCreatePermission[0]))
{
if($CheckCreatePermission[0]->create===1)
{
return 1;
}
return 0;
}
return 0;
}
protected function edit_permission($role_type_id,$module_id)
{
$CheckEditPermission = \DB::table('role_type_access')->where(['role_type_id'=> $role_type_id,'module_id'=>$module_id])->select('edit')->get();
if(!empty($CheckEditPermission[0]))
{
if($CheckEditPermission[0]->edit===1)
{
return 1;
}
return 0;
}
return 0;
}
protected function delete_permission($role_type_id,$module_id)
{
$CheckDeletePermission = \DB::table('role_type_access')->where(['role_type_id'=> $role_type_id,'module_id'=>$module_id])->select('delete')->get();
if(!empty($CheckDeletePermission[0]))
{
if($CheckDeletePermission[0]->delete===1)
{
return 1;
}
return 0;
}
return 0;
}
protected function view_permission($role_type_id,$module_id)
{
$CheckViewPermission = \DB::table('role_type_access')->where(['role_type_id'=> $role_type_id,'module_id'=>$module_id])->select('view')->get();
if(!empty($CheckViewPermission[0]))
{
if($CheckViewPermission[0]->view===1)
{
return 1;
}
return 0;
}
return 0;
}
protected function view_all_permission($role_type_id,$module_id)
{
$CheckLayoutPermission = \DB::table('role_type_access')
->join('modules', 'role_type_access.module_id', '=', 'modules.id')
->where(['role_type_access.role_type_id'=> $role_type_id,'role_type_access.view'=>1,'role_type_access.module_id'=>$module_id])
->select('role_type_access.module_id','role_type_access.view','role_type_access.create','role_type_access.edit','role_type_access.delete','modules.name','modules.label')->get();
return $CheckLayoutPermission;
// print_R($$CheckViewMenuPermission);
// echo count($CheckViewMenuPermission);
/* if(!empty($CheckViewPermission[0]))
{
if($CheckViewPermission[0]->view===1)
{
return 1;
}
return 0;
}
return 0;*/
}
public function getDownload($file_path,$file_name)
{
//PDF file is stored under project/public/download/info.pdf
$file= public_path().'/uploads/'.$file_path.'/'.$file_name;
$headers = array(
'Content-Type: application/pdf',
);
return \Response::download($file, $file_name, $headers);
}
public function updateTracker($tracked_date,$action)
{
$Globaltracks = \DB::table('global_tracks')->where('tracked_date', $tracked_date)->get();
if (count($Globaltracks) > 0) {
\DB::table('global_tracks')
->where('tracked_date', $tracked_date)
->increment($action,1,['updated_at'=>Carbon::now()->toDateTimeString()]);
} else {
$Globaltracks_id = \DB::table('global_tracks')->insert(
['tracked_date' => $tracked_date,$action => 1,'created_at'=>Carbon::now()->toDateTimeString()]);
}
}
}
Change Your Constructor Access Modifier to Public. It solve my problem.
public function __construct() {
$variable2 = "I am Data 2";
View::share ( 'variable2', $variable2 );
}
Update your SomeController with the below code:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class SomeController extends Controller
{
public function getIndex() {
echo "string";
}
}

Laravel 5.3 How to use Braintree\WebhookNotification

I am using Laravel 5.3 and trying to add new webhook event handler in WebhookController
Here is my controller
namespace App\Http\Controllers;
use Braintree\WebhookNotification;
use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use Log;
use App\Models\BraintreeMerchant;
class WebhookController extends CashierController
{
public function handleSubMerchantAccountApproved(WebhookNotification $notification)
{
if( isset($_POST["bt_signature"]) && isset($_POST["bt_payload"]))
{
$notification = Braintree_WebhookNotification::parse($_POST["bt_signature"], $_POST["bt_payload"]);
$notification->kind == Braintree_WebhookNotification::SUB_MERCHANT_ACCOUNT_APPROVED;
// true
$notification->merchantAccount->status;
// "active"
$notification->merchantAccount->id;
// "blue_ladders_store"
$notification->merchantAccount->masterMerchantAccount->id;
// "14ladders_marketplace"
$notification->merchantAccount->masterMerchantAccount->status;
}
}
}
but getting the following error message:
BindingResolutionException in Container.php line 763:
Target [Braintree\WebhookNotification] is not instantiable.
I've found the answer. this is how to implement Braintree webhook controller.
<?php
namespace App\Http\Controllers;
use Braintree\WebhookNotification;
use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use Illuminate\Http\Request;
class WebhookController extends CashierController
{
public function handleSubMerchantAccountApproved(Request $request)
{
$notification = WebhookNotification::parse($request->bt_signature, $request->bt_payload);
$merchantId = $notification->merchantAccount->id;
$result_merchant_status = $notification->merchantAccount->status;
}
}

Returning views using controller present somewhere else other than Views folder

How to return view present somewhere other than views folder from the controller in Laravel? I am making a project divided into modules. I want to return the view of the module from the controller of the main project. How to do it?
Because currently when I try to route to other links it shows:
MethodNotAllowedHttpException in compiled.php line 8895
Routes/web.php:
Route::get('/', function () {
return view('welcome');
});
Route::post('/navpage1',[
'uses'=>'ProjectController#nextpage1',
'as'=>'navpage1'
]);
Route::post('/navpage2',[
'uses'=>'ProjectController#nextpage2',
'as'=>'navpage2'
]);
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class ProjectController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function nextpage1()
{
return view('C:/xampp/htdocs/larve/app\Modules\Course_Entry\views\welcome');
}
public function nextpage2()
{
return view('C:/xampp/htdocs/larve/app\Modules\Log_in_blog_post\views\welcome');
}
}
App/Modules/ServiceProvider.php:
<?php namespace App\Modules;
class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
$modules = config("module.modules");
while (list(,$module) = each($modules)) {
if(file_exists(__DIR__.'/'.$module.'/web.php')) {
include __DIR__.'/'.$module.'/web.php';
}
if(is_dir(__DIR__.'/'.$module.'/Views')) {
$this->loadViewsFrom(__DIR__.'/'.$module.'/Views', $module);
}
}
}
public function register(){}
}
I know something is wrong with my controller, but i am just trying to figure out how to do it...
I have given more code here: Laravel program divided into modules
change the paths in app->config->view.php.
'paths' => [
realpath(base_path('app\Modules\Log_in_blog_post\views')),
],
then use in controller:- view('welcome').

Laravel Pass Authenticated to Every View

I need to pass a collection to every view; the collection contains the IDs of the items in the user's shopping cart. I've tried Service Providers and a BaseClass but neither worked as (apparently) Auth hasn't been registered at those points and only returns null.
What's the best way get records from an authenticated user and pass it to every view?
Edit: here's the relevant code
User.php
public static function getCart()
{
if (Auth::guest()) {
return [];
}
$collection = new \Illuminate\Database\Eloquent\Collection();
$collection = Auth::user()->cart()->pluck('post_id');
return $collection;
}
CartServiceProvider.php
namespace App\Providers;
use View;
use App\User;
use Illuminate\Support\ServiceProvider;
class CartServiceProvider extends ServiceProvider
{
public function boot()
{
View::share('cart', User::getCart());
}
public function register()
{
//
}
}
In any view...
<?php dd($cart); ?>
returns [] because Auth hasn't been registered yet, so the empty array is returned.
Found the answer on Laracasts and it seems to work quite well.
https://laracasts.com/discuss/channels/general-discussion/l5-service-provider-for-sharing-view-variables
From the OP #imJohnBon: "I managed to solve this issue by creating 2 files. First a ComposerServiceProvider which uses a wildcard to be applied to every view and not just particular views:"
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\View\Factory as ViewFactory;
class ComposerServiceProvider extends ServiceProvider {
public function boot(ViewFactory $view)
{
$view->composer('*', 'App\Http\ViewComposers\GlobalComposer');
}
public function register()
{
//
}
}
"And then the corresponding GlobalComposer where I share variables that should be available in all views:"
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
class GlobalComposer {
public function compose(View $view)
{
$view->with('currentUser', Auth::user());
}
}

Resources