laravel

时间:2017-08-04 07:06:53

标签: php forms laravel file controller

我是Laravel的新手,并试图学习表格。目前我正在尝试上传文件,我的创建页面如下所示:

<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>   


<body>
{!! Form::open(['action' => 'MovieController@create', 'enctype' => 'multipart/form-data']) !!}
    <div class="form-group">
    {{Form::label('name', 'Name')}}
    <br>
    {{Form::text('name')}}
    </div>
    <div class="form-group">
    {{Form::label('description', 'Description')}}
    <br>
    {{Form::textarea('description')}}
    </div>
    <div class="form-group">
    {{Form::label('release_date', 'Release Date')}}
    <br>
    {Form::date('release_date')}}
    </div>
    <div class="form-group">
    {{Form::label('country', 'Country')}}
    <br>
    {{Form::text('country')}}
    </div>
    <div class="form-group">
    {{Form::label('poster_name', 'Poster Image')}}
    {{Form::file('poster_name')}}
    </div>
    <div class="form-group">
    {{Form::label('file_name', 'Movie File')}}
    {{Form::file('file_name')}}
    </div>
    {{Form::submit('Submit',['class' => 'btn btn-primary'])}}
 {!! Form::close() !!}
</body>
</html>

正如您所看到的,我正在尝试制作'MovieController @ create'的动作。现在让我们看一下MovieController文件: 命名空间

App\Http\Controllers;

use Illuminate\Http\Request;
use App\Movie;

class MovieController extends Controller
{

    public function create(Request $request){
        $this->validate($request,[
            'poster_name' => 'required|image'
        ]);


        //Handle poster upload
        $imageName = $request->file('poster_name')->getClientOriginalName();
        $request->file('poster_name')->storeAs('public/images',$imageName);

        $videoName = $request->file('file_name')->getClientOriginalName();
        $request->file('file_name')->storeAs('public/videos',$videoName);

        $movie = new Movie;

        $movie->name = $request->name;
        $movie->description = $request->description;
        $movie->release_date = $request->release_date;
        $movie->country = $request->country;
        $movie->poster_name = $imageName;
        $movie->file_name = $videoName;

        $movie->save();

        $movies = Movie::all();

        return view('home',['movies' => $movies]); 
    }
}

一开始一切正常,但后来我在创建文件中进行了一些更改(仅限css和视觉更改),现在当我尝试转到该页面时,它会出现以下错误:

  

ErrorException   动作App \ Http \ Controllers \ MovieController @创建未定义。 (查看:C:\ xampp \ htdocs \ MagicMovie \ resources \ views \ movies \ create.blade.php)

有什么建议吗?

3 个答案:

答案 0 :(得分:5)

尝试做这样的事情,

在你的表格上

 {!! Form::open([  'action' => route('create_movie'), 
        'enctype' => 'multipart/form-data', 'method' => 'POST']) !!}

 {!! Form::open([  'action' => url('movie/create'), 
        'enctype' => 'multipart/form-data' , 'method' => 'POST' ]) !!}

在您的路线上(web.php

  Route::post('movie/create', ['uses' => 'MovieController@create', 'as' => 'create_movie']);

您还可以查看一些基本的Laravel路由here

答案 1 :(得分:1)

我想你可以试试这个:

Route::post('/storeMovie', 'MovieController@create')->name('storeMovie');
表单操作中的

{!! Form::open(['route' => 'storeMovie', 'enctype' => 'multipart/form-data']) !!}

希望这对你有用!!!

答案 2 :(得分:0)

你是绑定控制器方法直接形成,你不能这样做。

你应该为它写一条路线。

web.php

中的

Route::post('/add', 'MovieController@create')->name('create_movie');
form action

中的

'action' => 'create_movie'