需求是我们要将/?s=abc重定向到/letter/abc,这种请求如果我们不设置路由,那么会提示控制器不存在:app\controller\Abc。那么如果解决这个问题呢?
路由设置:
Route::get('letter/:name', 'index/letter'); Route::get('/:s', 'index/search');
注意这个上下顺序不能换,否则会出错。
控制器Index的代码如下:
<?php namespace app\controller; use think\facade\View; use think\facade\Db; class Index { public function index() { $letters = Db::table('letter')->orderRand()->limit(40)->select(); View::assign('letters',$letters); return View::fetch(); } public function search($s = ''){ return redirect('/letter/'.$s); } public function letter($name = '') { $content = Db::table('letter')->where('name',$name)->value('content'); if($content){ View::assign('name',$name); View::assign('content',$content); //上一篇 $front=Db::table('letter')->where("name",'<',$name)->order('ID desc')->limit('1')->value('name'); View::assign('front',$front); }else{ $content = ''; View::assign('name',$name); View::assign('content',$content); View::assign('front',''); } return View::fetch(); } }
0 个评论