我有一个"基本"问题,但我无法确定在哪里。
控制器和模型之间存在问题。
我上网了,我看到很多" Class' App \ Http \ Controllers \ App {something}'找不到"解决了问题,但没有一个对我有用。
可能是因为我做了一个复制过去的形式我的原生项目? 我只是在找"索引"部分。我无法继续工作,因为我无法看到自己在做什么。
myController的:
<?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
感谢您的帮助!!
答案 0 :(得分:0)
要解决您的问题,请更改:
<?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。
您不必像Laravel Session那样使用session_start()
。
您不必使用超级全局$_POST
,因为您可以使用Laravel的Request个对象。
您不应该处理模型中的请求数据,这是您的控制器和/或服务所针对的数据,此层(请求数据)不应该“流失”&#39;进入这样的模型。
您正在使用PDO语句,并完全绕过Laravel的Eloquent ORM。
我也看到很多条件要求,如果你使用Laravel的service container,你不应该做的事情。
通过所有这些,您实际上正在跳过Laravel的所有功能,并且您基本上只使用整个框架进行路由。