【问题标题】:line 5: Call to undefined function link_to_route()第 5 行:调用未定义的函数 link_to_route()
【发布时间】:2017-03-06 03:50:04
【问题描述】:

好的,所以我一直在使用 Laravel 设置我的第一个网站,但我一直遇到这个错误。我目前正在尝试设置索引页面(它是针对 CRUD 应用程序的),但此错误不断弹出。

FatalErrorException in 06f534e736fde409cb38fab481f04cd04f7fbca4.php line 5:
Call to undefined function link_to_route()

我的 web.php(路线):

<?php

Route::get('/', function () {
    return view('index');
});

Route::resource('users', 'UserController');
Route::get('/register', 'UserController@showUserRegistration');
Route::post('/register', 'UserController@saveUser');

我的用户控制器.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
class UserController extends BaseController {

  /**
   * Display a listing of the resource.
   *
   * @return Response
   */
  public function index()
  {
    $users = User::all();

    return View::make('users.index', compact('users'));
  }

  /**
   * Show the form for creating a new resource.
   *
   * @return Response
   */
  public function create()
  {
        return View::make('users.create');
  }

  /**
   * Store a newly created resource in storage.
   *
   * @return Response
   */
  public function store()
  {
    //
  }

  /**
   * Display the specified resource.
   *
   * @param  int  $id
   * @return Response
   */
  public function show($id)
  {
    //
  }

  /**
   * Show the form for editing the specified resource.
   *
   * @param  int  $id
   * @return Response
   */
  public function edit($id)
  {
    //
  }

  /**
   * Update the specified resource in storage.
   *
   * @param  int  $id
   * @return Response
   */
  public function update($id)
  {
    //
  }

  /**
   * Remove the specified resource from storage.
   *
   * @param  int  $id
   * @return Response
   */
  public function destroy($id)
  {
    //
  }

}

我的 index.blade.php:

@section('main')

<h1>All Users</h1>

<p>{{ link_to_route('users.create', 'Add new user') }}</p>

@if ($users->count())
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>Username</th>
        <th>Password</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Name</th>
            </tr>
        </thead>

        <tbody>
            @foreach ($users as $user)
                <tr>
                    <td>{{ $user->username }}</td>
          <td>{{ $user->password }}</td>
          <td>{{ $user->email }}</td>
          <td>{{ $user->phone }}</td>
          <td>{{ $user->name }}</td>
                    <td>{{ link_to_route('users.edit', 'Edit', array($user->id), array('class' => 'btn btn-info')) }}</td>
                    <td>
          {{ Form::open(array('method'
=> 'DELETE', 'route' => array('users.destroy', $user->id))) }}
                            {{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
                        {{ Form::close() }}
                    </td>
                </tr>
            @endforeach

        </tbody>

    </table>
@else
    There are no users
@endif

@stop

还有我的 user.blade.php(布局):

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

【问题讨论】:

    标签: php html mysql sql laravel


    【解决方案1】:

    要使用 link_to_route 助手,您需要拉取 "illuminate/html": "~5.0" 包。此处提供说明:http://laravel.com/docs/5.0/upgrade#upgrade-5.0 请参阅表单和 Html 助手部分。

    更新:如果您不想要 Html 助手,您可以使用以下方法:

    // For any routes
    <a href="{{ url('users/create') }}">Add new user</a>
    
    // For named routes
    <a href="{{ route('users.create') }}">Add new user</a>
    

    【讨论】:

    • ProviderRepository.php 第 146 行中的 FatalErrorException:找不到类“Collective\Html\HtmlServiceProvider”
    • @Ivannbn 请查看此Html 包说明laravelcollective.com/docs/5.0/html
    【解决方案2】:

    您可以使用 HTML 代码代替 link_to_route()

    <a href="{{ url('/register') }}">Add New User</a>
    

    或者,

    <a href="{{ action('UserController@showUserRegistration') }}">Add New User</a>
    

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 2016-09-18
      • 2014-11-02
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多