php - Laravel 路由方法问题

标签 php html laravel

基本信息

我使用 Laravel6.0 开发一个简单的 Web 应用程序。
我在 create2.blade.php ( Blade 文件)文件中制作了图像发布表单。
但我的表单似乎存在路由问题。

问题

The error message that annoying me.

我的代码

路线/web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('login');

Route::group(['middleweare' => 'auth'], function () {
    Route::get('/', 'StoriesController@index');
    Route::post('/', 'StoriesController@index');

    Route::post('/stories/create', 'StoriesController@store');
    Route::post('/stories/create', 'StoriesController@upload');
    Route::get('/stories/create', 'StoriesController@add');
    Route::post('/stories/create', 'StoriesController@add');
});

Route::group(['middleweare' => 'auth','name'=>'profile'], function () {
    Route::get('/profile/edit', 'ProfileController@edit');
    Route::get('/profile/create', 'ProfileController@add');
    Route::post('/profile/create', 'ProfileController@add');
    Route::post('/profile/create', 'ProfileController@store');
    Route::post('/profile/create', 'ProfileController@upload');
});

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();

应用程序/Http/Controllers/StoriesController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Story;
use Auth;
use App\Posts;
use App\History;
use App\Attachment;
use Carbon\Carbon;
use Storage;

class StoriesController extends Controller
{

  public function __construct()
    {
      $this->middleware('auth');
    }

  public function index(Request $request)
      {
        $images = Attachment::all();

        return view('stories.index2', compact('images'));
      }

  public function add()
    {
      return view('stories.create2');
    }


  public function store(Request $request)
  {

    $d = new \DateTime();
    $d->setTimeZone(new \DateTimeZone('Asia/Tokyo'));
    $dir = $d->format('Y/m');
    $path = sprintf('public/images/%s', $dir);


    $data = $request->except('_token');

    foreach ($data['images'] as $k => $v) {

      $filename = '';


      $attachments = Attachment::take(1)->orderBy('id', 'desc')->get();

      foreach ($attachments as $attachment) {

        $filename = $attachment->id + 1 . '_' . $v->getClientOriginalName();
      }
      unset($attachment);

      if ($filename == false) {
        $filename = 1 . '_' . $v->getClientOriginalName();
      }

      $v->storeAs($path, $filename);

      $attachment_data = [
        'path' => sprintf('images/%s/', $dir),
        'name' => $filename
      ];

      $a = new Attachment();
      $a->fill($attachment_data)->save();
    }

    unset($k, $v);

    return redirect('/');
  }


  public function upload(Request $request)
    {
      $this->validate($request, [
        'file' => [
          'required',
          'file',
          'image',
          'mimes:jpeg,png',
        ]
      ]);

      if ($request->file('file')->isValid([])) {
        $path = $request->file->store('public');
        return view('stories.index2')->with('filename', basename($path));
      } else {
        return redirect('/')
          ->back()
          ->withInput()
          ->withErrors();
      }
    }
}

资源/ View /故事/create2.blade.php

@extends('layouts.front2')
@section('title','StoryCreate')

@section('content')
<link href="{{ asset('/css/main22.css' )}}" rel="stylesheet">

<div class="poststory">
    <h1>Post Story</h1>
</div>
@if ($errors->any())
<ul>
    @foreach($errors->all() as $error)
    <li>{{ $error }}</li>
    @endforeach
</ul>
@endif
<form action="{{ url('/') }}" method="POST" enctype="multipart/form-data">

    <div class="form">
        <label for="photo" class="file">Choose Image or Video</label>
        <div class="post">
            <input type="file" class="here" name="images[]">

        </div>
    </div>
    <br>
    </div>

    {{ csrf_field() }}
    <div class="post">
        <div class="btn postbtn">
            <input type="submit" value="post" />
        </div>
    </div>
</form>

@endsection

资源/ View /故事/index2.blade.php

@extends('layouts.front2')
@section('title','mainpage')

@section('content')
<div class="profile">

    <div class="profileimg">
        @foreach ($images as $image)
        <img src="/storage/{{ $image->path . $image->name }}" style="height: 210px; width: 210px; border-radius: 50%;">
        @endforeach
    </div>

    <div class="name">
        @guest
        <a class="nav-link2" href="{{ route('register')}}">{{ __('Create Accout!')}}</a>
        @else
        <a id="navbarDropdown" class="nav-link2" href="#" role="button">
            {{Auth::user()->name}}<span class="caret"></span></a>



        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
            @csrf
        </form>
    </div>
    @endguest


    <div class="aboutme">
        You can write your profile here!You can write your profile here!You can write your profile here!
        You can write your profile here!You can write your profile here!You can write your profile here!
        You can write your profile here!You can write your profile here!You can write your profile here!
        You can write your profile here!You can write your profile here!You can write your profile here!
        You can write your profile here!You can write your profile here!You can write your profile here!
    </div>

</div>

<div class="new">

    <div class="newtitle">
        <h1>New</h1>
    </div>

    <div class="container1">

        @foreach ($images as $image)
        <img src="/storage/{{ $image->path . $image->name }}" class="images" style="height: 150px; width: 150px; border-radius: 50%;">
        @endforeach
        <div class="more">
            more...
        </div>
    </div>

</div>

<div class="stories">

    <div class="titlestories">
        <h1>Stories</h1>
    </div>

    <div class="container2">

        <div class="titleclose">
            <h2>#CloseFriends</h2>
        </div>

        @foreach ($images as $image)
        <img src="/storage/{{ $image->path . $image->name }}" class="images" style="height: 150px; width: 150px; border-radius: 50%;">
        @endforeach

        <div class="titlefollow">
            <h2>#Follows</h2>
        </div>


    </div>
</div>

{{ csrf_field() }}
@endsection

我尝试过的。

我使用此命令清除了缓存,

$php artisan route: clear

我使用

创建了一个新的缓存
$composer dump-autoload
$php artisan clear-compiled
$php artisan optimize
$php artisan config:cache

我使用此命令更新我的 Composer

$composer update


但一切都没有改变。

抱歉我的英语很糟糕。 等待您的评论和答复!

最佳答案

您正在使用 POST 方法将 from 提交到 {{ url('/') }},这意味着它是以 POST 形式提交的> 到 / 路由,并且您的 / 路由是由 web.php 中的 GET 方法定义的,这就是为什么您收到此错误

<form action="{{ url('/') }}" method="POST" enctype="multipart/form-data">

Route::get('/', 'StoriesController@index');

关于php - Laravel 路由方法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60462158/

相关文章:

javascript - jQuery 在滚动条上获取更多表格行

php - 使用preg_match替换字符串

html - 父容器空间不足时如何实现一定的重叠布局?

php - 此路由不支持 DELETE 方法。支持的方法 : GET, HEAD、POST。在拉拉维尔 7'

php - Laravel Schema onDelete 设置为空

symfony - 将 Sylius 包集成到 Laravel 项目中

php - Laravel 5.5 更改未经身份验证的登录重定向 url

php - 仅显示查询表中存在的 MySQL 列

javascript - 更新 XML 数据时使用 ajax 刷新 DIV

javascript - 调用网络服务 AngularJS