Php 如何在Laravel框架中正确设置路由和其他配置,并运行书本中的代码示例?
我正在尝试运行《拉拉威尔蓝图》一书中的代码,第4章:拉拉贡和比特纳米·拉威尔堆栈的个人博客 我将所有控制器、视图、模型和路由放在各自的应用程序文件夹中,设置数据库以便显示迁移的连接和表,但是,当我想要运行主页时,它会显示:找不到Post class。我无法返回该博客的主视图。请帮助:我在配置中遗漏了什么,如何运行关于Laragon和Bitnami Laravel的书中的示例,复制什么文件,什么保持不变,以及配置什么? 谢谢 }Php 如何在Laravel框架中正确设置路由和其他配置,并运行书本中的代码示例?,php,laravel,laravel-4,laravel-routing,bitnami,Php,Laravel,Laravel 4,Laravel Routing,Bitnami,我正在尝试运行《拉拉威尔蓝图》一书中的代码,第4章:拉拉贡和比特纳米·拉威尔堆栈的个人博客 我将所有控制器、视图、模型和路由放在各自的应用程序文件夹中,设置数据库以便显示迁移的连接和表,但是,当我想要运行主页时,它会显示:找不到Post class。我无法返回该博客的主视图。请帮助:我在配置中遗漏了什么,如何运行关于Laragon和Bitnami Laravel的书中的示例,复制什么文件,什么保持不变,以及配置什么? 谢谢 } 看起来像是一个名称空间错误,我在Begging也与之斗争过 请向我们
看起来像是一个名称空间错误,我在Begging也与之斗争过 请向我们展示您的Post类代码、routes.php文件和抛出错误的控制器
实际上,在执行此操作之前,请更改脚本中使用Post::for\App\Post::的所有位置,将laravel安装的根文件夹中的终端上的composer update设置为比应用文件夹高一级。没有任何帮助,计算机处于脱机状态,该怎么办?您忘记导入所需的所有类,名称空间在PHP中是新事物,因此您可以通过使用完整的route\App\Posts::或通过定义使用App\Posts来引用Post类;然后,您可以简单地使用Post::
<?php
class PostsController extends BaseController{
public function getIndex()
{
$posts = Posts::with('Author')->orderBy('id', 'DESC')->paginate(5);
return View::make('index')
->with('posts',$posts);
}
public function getAdmin()
{
return View::make('addpost');
}
public function postAdd()
{
Posts::create(array(
'title' => Input::get('title'),
'content' => Input::get('content'),
'author_id' => Auth::user()->id
));
return Redirect::route('index');
}
//the variable that sets the table name
protected $table = 'posts';
//the variable that sets which columns can be edited
protected $fillable = array('title','content','author_id');
public $timestamps = true;
public function Author(){
return $this->belongsTo('User','author_id');
}
<?php
Route::get('/', array('as' => 'index', 'uses' =>
'PostsController@getIndex')); Route::get('/admin', array('as' =>
'admin_area', 'uses' => 'PostsController@getAdmin'));
Route::post('/add', array('as' => 'add_new_post', 'uses' =>
'PostsController@postAdd')); Route::post('/login', array('as' =>
'login', 'uses' => 'UsersController@postLogin'));
Route::get('/logout', array('as' => 'logout', 'uses' =>
'UsersController@getLogout'));