路由、视图、控制器和模型行为异常、缓存
Routes, Views, Controllers, and Models behaving oddly, caching
我已经开始学习 Laravel (5.1) 并且一直在使用 laracasts 开始学习。
在掌握了基础知识后,我决定尝试自己的。我创建了一个项目并且做得很好,但后来遇到了麻烦。所以我决定创建一个新的演示项目来测试我是否破坏了一些配置。
我创建了一个迁移来为页面创建 table,其中包含 title
、slug
和 html
列。
我还有一个平面模型(由 php artisan make:model
生成),用于名为 Page
的页面。
我有我的 PageController
,它使用 App\Page
,并且有 index
和 show
方法,如下所示:
public function index()
{
return 'Pages Index';
}
public function show(Page $page)
{
return $page;
}
然后我的 routes.php
文件配置如下:
$router->bind('page', function($slug)
{
return App\Page::where('slug', $slug)->first();
});
$router->get('pages/', 'PagesController@index');
$router->get('pages/{page}', 'PagesController@show');
我的预期输出是 www.example.com/pages
输出 "Pages Index",wwww.example.com/pages/about
从数据库输出关于页面。
这成功了。然后我切换回我的主项目进行测试,但我无法让该项目运行。然后我转回我的演示项目。我测试了将我的 $router->get
切换到 $router->resource
,它没有用,所以我按 ctrl-z 键回到文件开始的方式,但该项目不再有效。我只是得到一个空的(新的)页面对象。
我试过使用php artisan cache:clear
、php artisan view:clear
、php artisan route:cache
都没有用。我也试过清除浏览器的缓存,但没有任何效果。
我做错了什么吗?或者有什么东西卡在 Laravel?
更新
根据要求,这是我的真实项目代码:
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 controller to call when that URI is requested.
|
*/
$router->bind('page', function($slug)
{
return App\Page::where('slug', $slug)->first();
});
$router->resource('page', 'PageController');
PageController.php
:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Page;
class PageController extends Controller
{
public function index()
{
$page = Page::where('slug', 'index')->first();
return view('page.index', compact('page'));
}
public function show(Page $page)
{
return $page;
}
}
我的索引视图显示了数据库中的正确页面,但是显示只在屏幕上显示 []
。
原来问题出在使用php artisan route:cache
,因为无法编译带有闭包的路由。 运行 php artisan route:clear
修复了问题。
我已经开始学习 Laravel (5.1) 并且一直在使用 laracasts 开始学习。
在掌握了基础知识后,我决定尝试自己的。我创建了一个项目并且做得很好,但后来遇到了麻烦。所以我决定创建一个新的演示项目来测试我是否破坏了一些配置。
我创建了一个迁移来为页面创建 table,其中包含 title
、slug
和 html
列。
我还有一个平面模型(由 php artisan make:model
生成),用于名为 Page
的页面。
我有我的 PageController
,它使用 App\Page
,并且有 index
和 show
方法,如下所示:
public function index()
{
return 'Pages Index';
}
public function show(Page $page)
{
return $page;
}
然后我的 routes.php
文件配置如下:
$router->bind('page', function($slug)
{
return App\Page::where('slug', $slug)->first();
});
$router->get('pages/', 'PagesController@index');
$router->get('pages/{page}', 'PagesController@show');
我的预期输出是 www.example.com/pages
输出 "Pages Index",wwww.example.com/pages/about
从数据库输出关于页面。
这成功了。然后我切换回我的主项目进行测试,但我无法让该项目运行。然后我转回我的演示项目。我测试了将我的 $router->get
切换到 $router->resource
,它没有用,所以我按 ctrl-z 键回到文件开始的方式,但该项目不再有效。我只是得到一个空的(新的)页面对象。
我试过使用php artisan cache:clear
、php artisan view:clear
、php artisan route:cache
都没有用。我也试过清除浏览器的缓存,但没有任何效果。
我做错了什么吗?或者有什么东西卡在 Laravel?
更新
根据要求,这是我的真实项目代码:
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 controller to call when that URI is requested.
|
*/
$router->bind('page', function($slug)
{
return App\Page::where('slug', $slug)->first();
});
$router->resource('page', 'PageController');
PageController.php
:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Page;
class PageController extends Controller
{
public function index()
{
$page = Page::where('slug', 'index')->first();
return view('page.index', compact('page'));
}
public function show(Page $page)
{
return $page;
}
}
我的索引视图显示了数据库中的正确页面,但是显示只在屏幕上显示 []
。
原来问题出在使用php artisan route:cache
,因为无法编译带有闭包的路由。 运行 php artisan route:clear
修复了问题。