我第一次尝试使用 Laravel 5.1。我已经成功安装了它,https://sub.example.com/laravel/public/
显示的是正确的内容。但是,我创建的视图却显示404错误页面未找到。
到目前为止,我已经完成了以下步骤:
在 laravel\app\Http\controllers\Authors.php
中创建了一个控制器。
下面是 Authors.php
文件中的代码:
<?php
class Authors_Controller extends Base_Controller {
public $restful = true;
public function get_index() {
return View::make('authors.index')
->with('title', 'Authors and Books')
->with('authors', Author::order_by('name')->get());
}
}
然后我在 laravel\resources\views\Authors\index.blade.php
中创建了一个视图。
这是 index.blade.php
文件的代码。
@layout('layouts.default')
@section('content')
Hello, this is a test
@endsection
然后我在 laravel\resources\views\layouts\default.blade.php
文件中创建了一个布局。
这是 default.blade.php
文件背后的代码。
<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
</head>
<body>
@if(Session::has('message'))
<p style="color: green;">{{ Session::get('message') }}</p>
@endif
@yield('content')
Hello - My first test
</body>
</html>
最后我在laravel\app\Http\routes.php
中创建了一个路由。
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('authors', array('as'=>'authors', 'uses'=>'authors@index'));
但出于某些原因,我一直收到404错误页面未找到的错误。
我通过取消注释这行代码启用了我的Apache 2.4.9上的mod_rewrite:
LoadModule rewrite_module modules/mod_rewrite.so
然后重新启动了Apache。
根据我在php_info()
输出中看到的,mod_rewrite 已启用。
Loaded Modules
core mod_win32 mpm_winnt http_core mod_so mod_php5 mod_access_compat mod_actions mod_alias mod_allowmethods mod_asis mod_auth_basic mod_authn_core mod_authn_file mod_authz_core mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_dir mod_env mod_include mod_isapi mod_log_config mod_mime mod_negotiation mod_rewrite mod_setenvif mod_socache_shmcb mod_ssl
我的当前的.htaccess文件看起来像这样“这是出厂默认设置”
Options -MultiViews
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
根据文档,我尝试将其更改为以下代码:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
然而,当我访问时仍然会出现404页面
https://sub.example.com/laravel/public/authors
我做错了什么?我该如何解决这个问题?