T
T
Twelfth Doctor2017-05-06 18:02:22
Laravel
Twelfth Doctor, 2017-05-06 18:02:22

Why won't Laravel controller start?

Hello! I'm trying to run the controller (without going to the page, namely the controller file itself). Does not work.
Fatal error: Class 'App\Http\Controllers\Controller' not found ...... on line 4

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class Site extends Controller
{
    //
}

Added after 2 minutes
Controller created via artisan
[email protected]:~/***/***/***$ php artisan make:controller Site

One of the forums said that
so that it can be run from the command line? that will never work. in the framework, the controller code is called as a result of the work of the request routing subsystem

So, in connection with this, the question arose:
1) Is it possible to use the DB class outside the laravel framework in such a context:
$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;

2) Why is this controller not working?
3) Is it possible to access the controller directly?
PS I switched to Laravel only for the sake of a very convenient implementation of working with the database, and if it can be used outside of Laravel as well as Eloquent ORM, I will only be happy.
Something like this:
<?php
use Illuminate\Database\DatabaseManager as DB;
$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D3lphi, 2017-05-06
@D3lphi


I'm trying to run the controller (without going to the page, namely the controller file itself).
Tau, that's it! That's why nothing worked for you yesterday. And I broke my head that you didn’t have it there. You would at least raise the language to a decent level for a start before switching to a framework. I explain for the especially gifted: you can’t just take and “launch a class” (I’m ashamed that I’m saying this)! As you were rightly told, the launch of the controller occurs as a result of a rather long process of booting (launching) the framework, the request goes through several stages before reaching the controller. If you just knew what class autoloading is, you would understand why the Fatal error occurs: Class 'App\Http\Controllers\Controller' not found ...... on line 4. Yes, because you are not connected file with this parent controller! And in general! Even if you had everything connected, then the class would not start like this by itself! Do you know why? No, I won’t say, if you please go and learn the basics of OOP.
Is it possible to access the controller directly?
NO, NO, NO, and again, NO! Why did you even need to do this???
Is it possible to use DB class outside of laravel framework in this context:
Using composer, install Eloquent ORM separately and work with it outside of Laravel.
PS I switched to Laravel only for the sake of a very convenient implementation of working with the database, and if it can be used outside of Laravel as well as Eloquent ORM, I will only be happy.
If you use the Framework only because of the ORM, and besides, you call it VERY CONVENIENT, then (no offense) I feel sorry for you.
And yes, finally learn the language.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question