Target [App\Http\Controllers\IndexController] is not instantiable. in laravel - 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";
}
}

Related

Laravel : need a controller to be called on all views

What i want is to load a sidebar with a controller inside, on my layouts/app.blade.php.
i have read that the best way is to load it on AppServiceProvider, so i tried this :
View::composer('layouts.app', function ($view) {
$data = \App\Http\Controllers\DeliveryController::index();
$view::share('Delivery',$data);
});
That works, but the DeliveryController::index gave me this error :
Using $this when not in object context
The way that really works is to forget the AppServiceProviers and to do it on every views controller like this :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Management\DeliveryManagement;
class WelcomeController extends Controller
{
protected $deliveryManagement;
protected $nbLastsDelivery = 3;
public function __construct(DeliveryManagement $deliveryManagement)
{
// $this->middleware('auth');
$this->deliveryManagement = $deliveryManagement;
}
public function index()
{
$deliveries = $this->deliveryManagement->getLasts($this->nbLastsDelivery);
return view ('welcome', compact('deliveries'));
}
}
Unfortunately i think AppServiceProviers is a better way, right ?
If someone can help me i would be very grateful !
EDIT :
I add code of DeliveryController and DeliveryManagement :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\DeliveryRequest;
use App\Management\DeliveryManagement;
class DeliveryController extends Controller
{
protected $deliveryManagement;
protected $nbLasts = 3;
public function __construct(DeliveryManagement $deliveryManagement)
{
$this->deliveryManagement=$deliveryManagement;
}
public function index()
{
$deliveries=$this->deliveryManagement->getLasts($this->nbLasts);
return $deliveries;
}
and :
<?php
namespace App\Management;
use App\Models\Delivery;
class DeliveryManagement extends ResourceManagement
{
protected $delivery;
public function __construct (Delivery $delivery)
{
$this->model=$delivery;
}
public function getLasts($limit)
{
$req = $this->model->orderBy('deliveries.id', 'desc')->skip(0)->take($limit)->get();
$i=0; $render = array();
foreach($req as $delivery)
{
if($i=0)
$render = [$delivery, 'latest'];
else
$render = [$delivery, 'older'];
$i++;
}
return $render;
}
}

Return 404 in Laravel RouteServiceProvider

I want to give a 404 response if index.php is found in the url.
example url is domain.com/index.php/about
expected :
return blade 404
I've tried this code but can't get the 404 display
class RouteServiceProvider extends ServiceProvider
{
protected $namespace = 'App\Http\Controllers';
public function boot()
{
parent::boot();
}
public function map()
{
$this->removeIndexPhpFromUrl();
}
protected function removeIndexPhpFromUrl()
{
$currentUrl = url()->current();
if(strpos($currentUrl, 'index.php') !== false){
return view('errors.404');
}
}
}
I use this code for solving this problem
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\File;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use App\Foundation\Support\Collectable;
class RouteServiceProvider extends ServiceProvider
{
protected $namespace = 'App\Http\Controllers';
public function boot()
{
parent::boot();
}
public function map()
{
$this->removeIndexPhpFromUrl();
}
protected function removeIndexPhpFromUrl()
{
$currentUrl = url()->current();
if(strpos($currentUrl, 'index.php') !== false){
$url = str_replace('/index.php','', $currentUrl);
return redirect()->to($url)->send();
}
}
}
But I can't use abort(404) in this RouteServiceProvider.php

How to implement event/listeners with repository pattern in laravel 5.4

I can't make listeners trigger action update, create or delete when I user patter repository.
Addionally I have added my code in order to help my to solve my problem.
TicketController.php
namespace App\Http\Organizer\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Events\Contracts\IEvent;
use App\Entities\Event;
class TicketController extends Controller
{
protected $IEvent;
public function __construct( IEvent $IEvent )
{
$this->IEvent = $IEvent;
}
public function checkFutbolType ($activityId)
{
// I need to listen this action here
$event = $this->IEvent->update(21927, ['title'=>'new title']);
}
}
My RepoEvent.php:
<?php
namespace App\Http\Events\Repositories;
use App\Http\Events\Contracts\IEvent
;
class RepoEvent implements IEvent
{
protected $model;
public function __construct($model)
{
$this->model = $model;
}
public function update($activityId, $params)
{
return $this->model->where('id', $activityId)->update($params);
}
}
My AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Entities\Event;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//event: creating
Event::creating(function (Event $event) {
return $event->creatingEvent();
});
//event: saving
Event::saving(function (Event $event) {
return $event->savingEvent();
});
//event: updating
Event::updating(function (Event $event) {
return $event->updatingEvent();
});
}
}
My interface IEvent.php:
<?php
namespace App\Http\Events\Contracts;
interface IEvent
{
public function update($activityId, $params);
}
My ServicesOrchestration.php:
<?php
namespace App\Http\Administration\Providers;
use App\Entities\Event;
use App\Http\Administration\Repositories\RepoEvent;
use Illuminate\Support\ServiceProvider;
class ServicesOrchestration extends ServiceProvider
{
public function boot()
{
}
public function register()
{
$this->app->bind('App\Http\Administration\Contracts\IEvent', function () {
return new RepoEvent(new Event());
});
}
}
My model Event.php
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
public function creatingUser() {
\Log::info('creating event');
}
public function savingUser() {
\Log::info('saving event');
}
public function updatingUser() {
\Log::info('updating event');
}
}
thanks in advance.thanks in advance.thanks in advance.thanks in advance.thanks in advance.thanks in advance
Here's the relevant snipped from the docs (scroll to mass updates):
When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.
For your code to work you need to first retrieve the actual model instance like below:
public function update($activityId, $params)
{
$instance = $this->model->find($activityId);
$instance->fill($params);
$instance->save();
}
This will have an additional cost of doing two queries instead of one and only being able to update a single model at a time.
A sidenote: You're passing a model instance to the repository but what you actually want is to pass a query builder instance:
$this->app->bind('App\Http\Administration\Contracts\IEvent', function () {
return new RepoEvent(Event::query());
});

laravel 5.4: seeding for inheritance model return error

I have created base model and extend all my model from base model in laravel 5.4. When i do db:seed i got error
Trying to get property of non-object
. Anyone know why it happens? it is db:seed did not support model inheritance.
Base Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
class BaseModel extends Model
{
public static function boot()
{
parent::boot();
static::creating(function($model)
{
$model->created_by = Auth::user()->id;
$model->updated_by = Auth::user()->id;
});
static::updating(function($model)
{
$model->updated_by = Auth::user()->id;
});
static::deleting(function($model)
{
$model->deleted_by = Auth::user()->id;
$model->save();
});
}
}
Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\SoftDeletes;
class Bank extends BaseModel
{
use SoftDeletes;
public static function boot()
{
parent::boot();
}
}
Seeder:
<?php
use Illuminate\Database\Seeder;
use App\Bank as Bank;
class BanksTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Bank::create( [
'name' => 'xxxxxxxx' ,
] );
}
}
Probably it has to do with Auth::user()->id. db:seed is executed in terminal and has no authenticated user, therefore Auth::user() will return NULL. Do a check before setting created_by and updated_by.
static::creating(function($model)
{
if (Auth::user())
{
$model->created_by = Auth::user()->id;
$model->updated_by = Auth::user()->id;
}
});
Hope this helps :)

Facade not found by AliasLoader in 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',

Resources