Trying to upgrade to Laravel and following this Laravel 5 upgrade
But when it comes to Filter routes for Sentry. I get this error:
FatalErrorException in RouteServiceProvider.php line 38: Class 'App\Providers\Session' not found
Related to this copied and pasted from previous L4 filter:
namespace App\Providers;
use Cartalyst\Sentry\Facades\Laravel\Sentry;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider {
protected $namespace = NULL; //using composer
public function boot(Router $router)
{
parent::boot($router);
Route::filter('Sentry', function(){
if (!Sentry::check()) {
Session::put('loginRedirect', Request::url());
return Redirect::guest('login');
}
});
}
}
The 'Session' is the issue.
Any help appreciated, thanks.
You have to import Session.
use Session;
Or prepend it with a backslash
\Session::put('loginRedirect', Request::url());
The same goes for Request and Redirect
Session facade exists under global namespace. Since your file is under App\Providers namespace, you have to use \Session::put('loginRedirect', Request::url()); instead of Session::put('loginRedirect', Request::url());.
Related
I am learning Laravel 9, and I got a problem Target class [Admin\DashboardController] does not exist. when using prefix routing. Is someone can help me to fix it?
this code on routes/web:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
this code on App\Http\Controllers\Admin\DashboardController:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index(Request $request){
return view('pages.admin.dashboard');
}
}
You've specified an incorrect namespace in your route. As per the error message:
Target class [Admin\DashboardController] does not exist
Laravel expects to find a DashboardController in the Admin namespace, however, you've defined your DashboardController with the namespace App\Http\Controllers\Admin.
Update the namespace on your route.
Route::prefix('admin')->namespace('App\Http\Controllers\Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
If you read the documentation, you will see that you must use another way:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
});
I've tried the following but the return is null.
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider {
public function boot() {
$routeList = Route::getRoutes();
foreach ($routeList as $value) {
dd($value->getPath());
}
}
}
My route file:
<?php
Route::namespace('admin')->group(function () {
Route::get('admin/post', 'PostController#index')->name('posts');
Route::get('admin/post/new', 'PostController#new')->name('post_new');
Route::post('admin/post/save', 'SubjectController#save')->name('post_save');
});
I tried several ways, but I can not list the routes created in the web.php routes file
This will provide every details about routes.
$routes = app('router')->getRoutes();
return $arrays=(array) $routes;
If you want to use them in your controller to use the programmatically, then you can access them through the Route Class via Route::getRoutes().
use \Route;
dd(Route::getRoutes());
To review/analyze the list of routes however, I just use the artisan command line in the root of your application:
php artisan route:list
If you have a bash, you can even look for specific routes with grep.
php artisan route:list |grep users
Hope this helps.
Thanks to all in advance.
I am trying to create Facades for my custom and common functions in laravel 5.0 also I don`t want to create controller for that so I am using Facades.
I have tried almost every tutorial but it do not help me.
Please help me to create facade without using Composer in Laravel 5.0.
Thanks again.
First of all you're creating a class of facade like this:
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class SomeFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'someService';
}
}
then You create a service class that hold your functionalities:
namespace App\Services;
class SomeService { ... }
Finally you have to register it and set an alias (not required) for it:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProivider extends ServiceProvider
{
(...)
public function register()
{
$this->app->singleton('someService', function () {
return new \App\Services\SomeService();
});
$this->app->alias('SomeServiceFacade', \App\Facades\SomeFacade::class);
}
}
Now you can call your methods from SomeService with:
SomeServiceFacade::someMethhod();
or
app('someService')->someMethhod();
My Controller for my class 'Article' cannot be found.
I am needing to fetch all entries from the article table.
I am able to use the DB:: facade to pull what's in the DB, but when I try using Article::all() I get:
Class 'App\Http\Controllers\Article' not found
in ArticleController.php line 15
at Application->Laravel\Lumen\{closure}()
Line 15 looks like:
$article = Article::all();
This is what I've tried so far, but with no success:
updated .env.example to .env and set up my DB credentials.
in bootstrap/app.php I've uncommented Dotenv::load(__DIR__.'/../');
In bootstrap/app.php I've uncommented $app->withFacades(); $app->withEloquent();
I've tried to use the full route of the controller in routes.php: $app->get('article', 'App\Http\Controllers\ArticleController#index');
My model directory is located under app->Models and has my Article.php model:
<?php
# app/Models/Article.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $table = 'articles';
protected $fillable = ['title', 'content'];
}
My Controller is ArticleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ArticleController extends Controller{
public function index(){
$article = Article::all();
return response()->json($article);
}
}
And my routes.php
<?php
$app->get('article', 'ArticleController#index');
I really appreciate any help on this error. I've unfortunately spent the better part of 2 days on this.
Thanks.
You need to have the proper namespace. Your Article model is in the App\Models namespace so you need to add this to the top of your controller:
use App\Models\Article;
Just run composer update. a new autoload classmap will be generated and all your new composer will be ready to autoload after successful update.
Thanks for your responses and for your answer #Thomas. I followed your response and was able to add the correct namespace in my controller.
I'm trying to deepen my knowlade in laravel architecture.
I have a search engine (elastic search for the sake of the example), but this search engine might change in the future. So im trying to write a container for this, so in case i'll change the engine in the future, i will have to change only the container. (I believe the termenology is factory design?)
I have created a provider app/providers/DataFromSearchEngine.php that looks like this:
use Illuminate\Support\ServiceProvider;
class DataFromSearchEngine extends ServiceProvider {
public function boot()
{
//
}
public function register()
{
$this->app->singleton('SearchEngine', function($app) {
return new elasticSearch;
});
}
}
Then i registered it in the providers array in config/app.php .
'providers' => [
// providers...
'App\Providers\DataFromSearchEngine'
],
The next step is to call SearchEngine from my controller:
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class SearchController extends Controller {
protected $searchEngine;
public function __construct() {
$this->searchEngine = $this->app->make('SearchEngine');
}
}
But all these yields: Undefined property: App\Http\Controllers\SearchController::$app
Can someone explain what i'm missing?
Instead of using $this->app try using app().
This is because non of the inherited controller classes, i.e. App\Http\Controllers\Controller or Illuminate\Routing\Controllers\Controller have an app property on them.
As a note you can use app('SearchEngine') which is the equivalent of app()->make('SearchEngine') as a shortcut to making your object.
I had this issue when trying to create a service provider. I registered my service provider in AppServiceProvider.php but was still getting this same error. The issue was that in my ServiceProvider I needed to add extends ServiceProvider to my class. Seems simple but is often forgotten.