【发布时间】:2015-10-13 01:47:15
【问题描述】:
我是第一次尝试使用 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 文件看起来像这样“这是出厂默认设置”
选项 - 多视图
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]
我也尝试根据documentation将其更改为以下代码:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
但是,当我去时,我仍然得到 404 页面
https://sub.example.com/laravel/public/authors
我做错了什么? 我该如何解决这个问题?
【问题讨论】:
标签: php apache .htaccess laravel mod-rewrite