无法访问 Laravel 路由中的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26133517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:13:06  来源:igfitidea点击:

Can't access URL in Laravel routes

phpapache.htaccessmod-rewritelaravel

提问by Jerielle

I am studying Laravel right now and I am doing it by creating a simple CRUD (CREATE-READ-UPDATE-DELETE). This is my way of learning new framework. Before I am using CodeIgniter but I found out that Laravel is much better than CodeIgniter and it has a lot of features.

我现在正在学习 Laravel,我通过创建一个简单的 CRUD (CREATE-READ-UPDATE-DELETE) 来完成它。这是我学习新框架的方式。在我使用 CodeIgniter 之前,我发现 Laravel 比 CodeIgniter 好得多,而且它有很多功能。

I am following this tutorial

我正在关注本教程

http://www.allshorevirtualstaffing.com/developing-crud-applications-in-laravel-4/

http://www.allshorevirtualstaffing.com/developing-crud-applications-in-laravel-4/

And I am in the displaying of CREATE view. But I can't access the routes of it.

我在 CREATE 视图的显示中。但我无法访问它的路线。

This is my folder structure

这是我的文件夹结构

wamp
  www
    mylaravel
      app
      bootstrap
      public 
      vendor

So far I have these codes:

到目前为止,我有这些代码:

Controller

控制器

<?php

class EmployeesController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $employees = Employee::all();
        return View::make('index', compact('employees'));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('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)
    {
        //
    }

    public function handleCreate() {

        $employee = new Employee;
        $employee->first_name = Input::get('first_name');
        $employee->last_name = Input::get('last_name');
        $employee->email = Input::get('email');
        $employee->save();
        return Redirect::action('EmployeesController@index');

    }


}

Model

模型

<?php

    class Employee extends Eloquent 
    {

    }

?>

VIEW (index.blade.php)

查看(index.blade.php)

@extends('layout')
@section('content')
<div class="page-header" style="border: 1x solid #0077b3; text-align-center">
    <h1>EMS <small> Better Employee Management </small> </h1>
    </p></div>
    <div class="panel panel-default">
        <div class="panel-body">
            <a href="{{ action('EmployeesController@create') }}" class="btn btn-info">Add new employee</a>
        </div>

        @if ($employees->isEmpty())
            There are no employees! :(
        @else
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Email</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($employees as $employee)
                        <tr>
                            <td> {{ $employee->first_name }} </td>
                            <td> {{ $employee->last_name }} </td>
                            <td> {{ $employee->email }} </td>
                            <td>
                                <a href="{{ action('EmployeesController@edit', $employee->id) }}" class="btn btn-default">Edit</a>
                                <a href="{{ action('EmployeesController@delete', $employee->id) }}" class="btn btn-danger">Delete</a>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        @endif
        @stop
    </div>
</div>

VIEW (layout.blade.php)

查看(layout.blade.php)

<!DOCTYPE html>
<html>
    <head>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
        <style type="text/css">
            table form { margin-bottom: 0; }
            form ul { margin-left: 0; list-style: none; }
            .error { color: red; font-style: italic; }
            body { padding-top: 20px; }
        </style>
    </head>
    <body>
        <div class="container">
            @if (Session::has('message'))
                <div class="flash alert">
                    <p>{{ Session::get('message') }}</p>
                </div>
            @endif
            @yield('content')
        </div>
    </body>
</html>

VIEW (create.blade.php)

查看 (create.blade.php)

@extends('layout')
@section('content')
<div class="page-header" style="border: 1px solid #0077b3;">
    <h1>Add New Employee</h1>
    @if( $errors->count() > 0 )
    <div class="alert alert-danger">
        <ul>
            @foreach( $errors->all() as $message)</p>
                <li>{{ $message }}</li>
            @endforeach
        </ul>
    </div>
    @endif
    <form action="{{ action('EmployeesController@handleCreate') }}" method="post" role="form">
        <div class="form-group">
            <label for="first_name">First Name</label>
            <input type="text" class="form-control" name="first_name" />
        </div>
        <div class="form-group">
            <label for="last_name">Last Name</label>
            <input type="text" class="form-control" name="last_name" />
        </div>
        <div class="form-group">
            <label for="email">Last Name</label>
            <input type="text" class="form-control" name="email" />
        </div>
        <input type="submit" value="Add" class="btn btn-primary" />
        <a href=" action('EmployeesController@index') " class="btn btn-link">Cancel</a>
    </form>
    @stop
</div>

ROUTES (routes.php)

路线 (routes.php)

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
/*
Route::get('/', function()
{
    return View::make('hello');
});
*/

Route::model('employee', 'Employee');
Route::get('/', 'EmployeesController@index');
Route::get('/create', 'EmployeesController@create');
Route::get('/edit/{$employee}', 'EmployeesController@edit');
Route::get('/delete/{$employee}', 'EmployeesController@delete');

Route::post('/create', 'EmployeesController@handleCreate');
Route::post('/edit', 'EmployeesController@handleEdit');
Route::post('/delete', 'EmployeesController@handleDelete');

When I am accessing this URL:

当我访问此 URL 时:

http://localhost/mylaravel/public/

It display the index page with employee table

它显示带有员工表的索引页面

But when I click the ADD NEW EMPLOYEE

但是当我点击添加新员工时

It goes to this page

它转到这个页面

http://localhost/mylaravel/public/create

And I have this error:

我有这个错误:

Not Found

The requested URL /mylaravel/public/create was not found on this server.

Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12 Server at localhost Port 80

I don't know where did I go wrong. Can you help me with this?

我不知道我哪里出错了。你能帮我解决这个问题吗?

回答by Marcin Nabia?ek

You need to have mod_rewrite enabled in your Apache configuration file - in httpd.confline:

您需要在 Apache 配置文件中启用 mod_rewrite -httpd.conf行内:

LoadModule rewrite_module modules/mod_rewrite.so

should be without #at the beginning. After change you should restart your server.

#一开始应该是没有的。更改后,您应该重新启动服务器。

You also need to have in publicdirectory default .htaccessfile from Laravel - I mention it because on webservers sometimes .htaccessfiles as hidden and if you copy project from FTP you might not copied them.

您还需要在Laravelpublic目录中拥有默认.htaccess文件 - 我提到它是因为在网络服务器上有时.htaccess文件是隐藏的,如果您从 FTP 复制项目,您可能不会复制它们。