Class 'App\Http\Controllers\App\Article' 未找到 Laravel 5.6
Class 'App\Http\Controllers\App\Article' not found Laravel 5.6
我有一个 "basic" 问题,但我不知道出在哪里。
控制器和模型之间有问题。
我上网,看到很多 "Class 'App\Http\Controllers\App{something}' not found" 已解决的问题,但 none 对我有用。
难道是因为我复制了我的原生项目?
我只是在寻找 "index" 部分。我无法处理其余部分,因为我看不到自己在做什么。
我的控制器:
<?php
namespace App;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Article;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
session_start();
$article = App\Article::Blog();
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
session_start();
// si je reçois des données de formulaire,
if (isset($_POST['titre'], $_POST['description'], $_POST['contenu'])
){
$admin = Add();
// si la création a réussi,
if ($add) {
//rediriger vers
header('location: /Chef/Controleur/controleur-blog.php');
echo "Well done!". '<br>';
echo "You have add an article!";
}
}
else {
// afficher le formulaire
echo "Error. Please try again.";
require __DIR__.'/Controleur/controleur_piedpage.php';
}
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
require __DIR__.'/../Modele/modele.php';
$article = Article();
// si je reçois des données de formulaire,
if (isset($_POST['titre'], $_POST['description'], $_POST['contenu'])){
$admin = Edit();
// si la création a réussi,
if ($edit) {
//rediriger vers
header('location: /Chef/Controleur/controleur-blog.php');
echo "You have edit an article!";
}
}
else {
// afficher le formulaire
require __DIR__.'/Controleur/controleur-entete.php';
require __DIR__.'/Controleur/controleur-recherche.php';
require __DIR__.'/../Vue/edit.php';
echo "Error. Please try again.";
require __DIR__.'/Controleur/controleur_piedpage.php';
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
require __DIR__.'/../Modele/modele.php';
$admin = Delete();
$article = Article();
if ($delete) {
//rediriger vers
header('location: /Chef/Controleur/controleur-blog.php');
}
else {
// afficher le formulaire
require __DIR__.'/Controleur/controleur-entete.php';
require __DIR__.'/Controleur/controleur-recherche.php';
require __DIR__.'/../Vue/delete.php';
echo "Error. Please try again.";
require __DIR__.'/Controleur/controleur_piedpage.php';
}
}
}
我的模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
function Blog() {
if(isset($_POST['register'])) if(('role'== 0) && ('role' == 1)) {
$todoread = [];
$pdo = prepareStatement('SELECT * FROM article ORDER BY id DESC LIMIT 100');
$pdo->execute();
$todoread = $pdo->fetchAll(PDO::FETCH_ASSOC);
return $todoread;
}
}
function Edit(){
$pdo_statement = prepareStatement('UPDATE article
SET titre=:titre, description=:description, contenu =:contenu
WHERE id=:id');
$pdo_statement->bindParam(':id', $_GET['id']);
$pdo_statement->bindParam(':titre', $_POST['titre']);
$pdo_statement->bindParam(':description', $_POST['description']);
$pdo_statement->bindParam(':contenu', $_POST['contenu']);
$pdo_statement->execute();
return true;
}
function Article(){
if(isset($_GET['id'])) {
$sql = "SELECT * FROM article WHERE id = :id";
$article = prepareStatement($sql);
$article->bindParam(':id', $_GET['id']);
$article->execute();
return $article->fetch();
}
}
function Delete() {
if (isset($_POST['submit'])) {
$pdo_statement = prepareStatement('DELETE FROM article WHERE id =:id');
$pdo_statement->bindParam(':id', $_GET['id']);
$pdo_statement->execute();
$pdo_statement->fetchAll(PDO::FETCH_ASSOC);
return true;
}
}
function Add(){
if (isset($_POST['submit'])) {
$pdo_statement = prepareStatement(
'INSERT INTO article (titre, description, contenu)
VALUES (:titre, :description, :contenu)');
$pdo_statement->bindParam(':titre', $_POST['titre']);
$pdo_statement->bindParam(':description', $_POST['description']);
$pdo_statement->bindParam(':contenu', $_POST['contenu']);
$pdo_statement->execute();
return true;
}
}
}
视图:
@extends('layouts.app')
@section('title', 'Blog')
@section('content')
<br>
<br>
<br>
<?php if(!isset($_SESSION['nom'])): ?>
<h1 class="center"> Welcome to my blog ! </h1>
<h2 class="center"> Tips, tutorials and more on chatbots, artificials intelligence </h2>
<p>
To access the blog, please register yourself <br>
You'll find the link to the registration <a href="/Chef/Controleur/controleur-registration.php"> here</a> or you can login <a href="/Chef/Controleur/controleur-login.php"> here</a>.
</p>
<?php else: ?>
<h1 class="center"> Welcome to my blog <?= $_SESSION ['nom'] ?> ! </h1>
<h2 class="center"> Enjoy the tips and tutorials of this blog! </h2>
<br>
<button class="ui small black button"> <a href="/Chef/Controleur/controleur-add.php"> Add an article </button> </a>
<br>
<?php
foreach ($blog as $ligne) {
?>
<br>
<button class="ui small black button"> <a href="/Chef/Controleur/controleur-edit?id=<?php echo $ligne["id"]; ?>"> Edit the article</button> </a>
<button class="ui small black button"> <a href="/Chef/Controleur/controleur-delete?id=<?php echo $ligne["id"]; ?>"> Delete the article </button> </a> <br>
<?php echo $ligne['titre'].'<br>'; ?><br>
<?php echo $ligne['description'].'<br>'; ?><br>
<?php echo $ligne['contenu'].'<br>'; ?> <br> <br> <!-- créer un template : https://openclassrooms.com/courses/adoptez-une-architecture-mvc-en-php/creer-un-template-de-page !-->
<?php
}
?>
<br>
<?php endif; ?>
@endsection
感谢您的帮助!!
要解决您的问题,请更改:
<?php
namespace App;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Article;
在您的控制器中:
<?php
namespace App\Http\Controllers; // <- removing duplicate namespace
use Illuminate\Http\Request;
use App\Article; // <- Case sensitive, use App\, not app\
除此之外,我真的鼓励你阅读 the documentation of Laravel,因为这段代码并没有真正使用 Laravel。
您不必使用 session_start()
,因为在 Laravel 中您有 Session。
您不必像 $_POST
那样使用超全局变量,因为您可以使用 Laravel 的 Request 对象。
你不应该在你的模型中处理请求数据,那是你的控制器 and/or 服务的目的,这一层(请求数据)不应该像这样 'bleed' 到你的模型中。
您正在使用 PDO 语句,完全绕过了 Laravel 的 Eloquent ORM。
我还看到很多条件要求,如果您使用 Laravel 的 service container,则不应这样做。
有了所有这些,您几乎跳过了 Laravel 的所有功能,您基本上只是将整个框架用于路由。
我有一个 "basic" 问题,但我不知道出在哪里。
控制器和模型之间有问题。
我上网,看到很多 "Class 'App\Http\Controllers\App{something}' not found" 已解决的问题,但 none 对我有用。
难道是因为我复制了我的原生项目? 我只是在寻找 "index" 部分。我无法处理其余部分,因为我看不到自己在做什么。
我的控制器:
<?php
namespace App;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Article;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
session_start();
$article = App\Article::Blog();
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
session_start();
// si je reçois des données de formulaire,
if (isset($_POST['titre'], $_POST['description'], $_POST['contenu'])
){
$admin = Add();
// si la création a réussi,
if ($add) {
//rediriger vers
header('location: /Chef/Controleur/controleur-blog.php');
echo "Well done!". '<br>';
echo "You have add an article!";
}
}
else {
// afficher le formulaire
echo "Error. Please try again.";
require __DIR__.'/Controleur/controleur_piedpage.php';
}
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
require __DIR__.'/../Modele/modele.php';
$article = Article();
// si je reçois des données de formulaire,
if (isset($_POST['titre'], $_POST['description'], $_POST['contenu'])){
$admin = Edit();
// si la création a réussi,
if ($edit) {
//rediriger vers
header('location: /Chef/Controleur/controleur-blog.php');
echo "You have edit an article!";
}
}
else {
// afficher le formulaire
require __DIR__.'/Controleur/controleur-entete.php';
require __DIR__.'/Controleur/controleur-recherche.php';
require __DIR__.'/../Vue/edit.php';
echo "Error. Please try again.";
require __DIR__.'/Controleur/controleur_piedpage.php';
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
require __DIR__.'/../Modele/modele.php';
$admin = Delete();
$article = Article();
if ($delete) {
//rediriger vers
header('location: /Chef/Controleur/controleur-blog.php');
}
else {
// afficher le formulaire
require __DIR__.'/Controleur/controleur-entete.php';
require __DIR__.'/Controleur/controleur-recherche.php';
require __DIR__.'/../Vue/delete.php';
echo "Error. Please try again.";
require __DIR__.'/Controleur/controleur_piedpage.php';
}
}
}
我的模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
function Blog() {
if(isset($_POST['register'])) if(('role'== 0) && ('role' == 1)) {
$todoread = [];
$pdo = prepareStatement('SELECT * FROM article ORDER BY id DESC LIMIT 100');
$pdo->execute();
$todoread = $pdo->fetchAll(PDO::FETCH_ASSOC);
return $todoread;
}
}
function Edit(){
$pdo_statement = prepareStatement('UPDATE article
SET titre=:titre, description=:description, contenu =:contenu
WHERE id=:id');
$pdo_statement->bindParam(':id', $_GET['id']);
$pdo_statement->bindParam(':titre', $_POST['titre']);
$pdo_statement->bindParam(':description', $_POST['description']);
$pdo_statement->bindParam(':contenu', $_POST['contenu']);
$pdo_statement->execute();
return true;
}
function Article(){
if(isset($_GET['id'])) {
$sql = "SELECT * FROM article WHERE id = :id";
$article = prepareStatement($sql);
$article->bindParam(':id', $_GET['id']);
$article->execute();
return $article->fetch();
}
}
function Delete() {
if (isset($_POST['submit'])) {
$pdo_statement = prepareStatement('DELETE FROM article WHERE id =:id');
$pdo_statement->bindParam(':id', $_GET['id']);
$pdo_statement->execute();
$pdo_statement->fetchAll(PDO::FETCH_ASSOC);
return true;
}
}
function Add(){
if (isset($_POST['submit'])) {
$pdo_statement = prepareStatement(
'INSERT INTO article (titre, description, contenu)
VALUES (:titre, :description, :contenu)');
$pdo_statement->bindParam(':titre', $_POST['titre']);
$pdo_statement->bindParam(':description', $_POST['description']);
$pdo_statement->bindParam(':contenu', $_POST['contenu']);
$pdo_statement->execute();
return true;
}
}
}
视图:
@extends('layouts.app')
@section('title', 'Blog')
@section('content')
<br>
<br>
<br>
<?php if(!isset($_SESSION['nom'])): ?>
<h1 class="center"> Welcome to my blog ! </h1>
<h2 class="center"> Tips, tutorials and more on chatbots, artificials intelligence </h2>
<p>
To access the blog, please register yourself <br>
You'll find the link to the registration <a href="/Chef/Controleur/controleur-registration.php"> here</a> or you can login <a href="/Chef/Controleur/controleur-login.php"> here</a>.
</p>
<?php else: ?>
<h1 class="center"> Welcome to my blog <?= $_SESSION ['nom'] ?> ! </h1>
<h2 class="center"> Enjoy the tips and tutorials of this blog! </h2>
<br>
<button class="ui small black button"> <a href="/Chef/Controleur/controleur-add.php"> Add an article </button> </a>
<br>
<?php
foreach ($blog as $ligne) {
?>
<br>
<button class="ui small black button"> <a href="/Chef/Controleur/controleur-edit?id=<?php echo $ligne["id"]; ?>"> Edit the article</button> </a>
<button class="ui small black button"> <a href="/Chef/Controleur/controleur-delete?id=<?php echo $ligne["id"]; ?>"> Delete the article </button> </a> <br>
<?php echo $ligne['titre'].'<br>'; ?><br>
<?php echo $ligne['description'].'<br>'; ?><br>
<?php echo $ligne['contenu'].'<br>'; ?> <br> <br> <!-- créer un template : https://openclassrooms.com/courses/adoptez-une-architecture-mvc-en-php/creer-un-template-de-page !-->
<?php
}
?>
<br>
<?php endif; ?>
@endsection
感谢您的帮助!!
要解决您的问题,请更改:
<?php
namespace App;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Article;
在您的控制器中:
<?php
namespace App\Http\Controllers; // <- removing duplicate namespace
use Illuminate\Http\Request;
use App\Article; // <- Case sensitive, use App\, not app\
除此之外,我真的鼓励你阅读 the documentation of Laravel,因为这段代码并没有真正使用 Laravel。
您不必使用 session_start()
,因为在 Laravel 中您有 Session。
您不必像 $_POST
那样使用超全局变量,因为您可以使用 Laravel 的 Request 对象。
你不应该在你的模型中处理请求数据,那是你的控制器 and/or 服务的目的,这一层(请求数据)不应该像这样 'bleed' 到你的模型中。
您正在使用 PDO 语句,完全绕过了 Laravel 的 Eloquent ORM。
我还看到很多条件要求,如果您使用 Laravel 的 service container,则不应这样做。
有了所有这些,您几乎跳过了 Laravel 的所有功能,您基本上只是将整个框架用于路由。