第5行:调用未定义的函数link_to_route()

时间:2016-10-24 11:27:41

标签: php html mysql sql laravel

好吧,所以我一直在用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');

我的usercontroller.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',
    ];
}

2 个答案:

答案 0 :(得分:2)

使用link_to_route帮助器,你需要拉动&#34;照亮/ html&#34;:&#34; ~5.0&#34;包。可在此处获取相关说明:http://laravel.com/docs/5.0/upgrade#upgrade-5.0请参阅表格&amp; Html Helpers部分。

更新:如果您不想要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>

答案 1 :(得分:0)

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

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

,或者

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