I am new to Laravel, and it's my first time creating a controller. I've also searched for hours for similar problems, but could not find any solution that will work for me.
When I was not using a controller yet, I was able to display a page using this code inside app/Providers/RouteServiceProvider.php:
Route::get('/', function(){
if(View::exists('pages.index'))
return view('pages.index');
else
return view('errors.404',['xp'=>'pages/index']);
});
The problem started when I created a controller and substituted the above block of code into this:
Route::get('/', 'SiteController@index');
After using the above code, I got this error:
ReflectionException in Container.php line 749: Class SiteController does not exist
Here are my complete codes:
inside app/Providers/RouteServiceProvider.php:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\View;
class RouteServiceProvider extends ServiceProvider{
protected $namespace = 'App\Http\Controllers';
public function boot(){
parent::boot();
}
public function map(){
$this->mapApiRoutes();
$this->mapWebRoutes();
}
protected function mapWebRoutes(){
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
/*site view*/
Route::get('/', 'SiteController@index');
}
protected function mapApiRoutes(){
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
}
inside app/Http/Controllers/SiteController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class SiteController extends Controller{
public function index(){
$this->check_page(['pages.index']);
}
public function check_page($page){
$xp = str_replace('.','/',$page);
if(View::exists($page))
return view($page);
else
return view('errors.404',$xp);
}
}
And maybe I'm just plain dumb (fine), but I can't find anything from this "Greek" Laravel Documentation which would help me at all.
I hope someone out there has encountered this before and could share their solution. Thanks a lot.
first run the command composer dump-autoload
If its not work, than follow those steps:
step-1: create route inside routes\web.php
Route::get('/', 'SiteController@index');
step-2: create a controller using cmd
like php artisan make:controller SiteController
inside: app/Providers/RouteServiceProvider.php should be like this:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapWebRoutes();
$this->mapApiRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::group([
'middleware' => ['api', 'auth:api'],
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
}
Inside: app/Http/Controllers/SiteController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class SiteController extends Controller
{
public function index() {
return view('pages.index');
}
}
Create view page inside the:
resources/views/pages/index.blade.php
Run server using cmd php artisan serve
Hope this help's you!