Php Laravel 4 URL重写
我有一个控制当前页面用户所在位置的路由:Php Laravel 4 URL重写,php,url-rewriting,laravel,Php,Url Rewriting,Laravel,我有一个控制当前页面用户所在位置的路由: Route::group(array('prefix'=>'v1'), function(){ Route::resource('page', 'PageController', ['only'=>['index','show']]); }); 通过上面的代码,这意味着要进入主页,我需要键入类似http://localhost/public/v1/page。所以我需要去http://localhost/public/v1因此,我将上
Route::group(array('prefix'=>'v1'), function(){
Route::resource('page', 'PageController', ['only'=>['index','show']]);
});
通过上面的代码,这意味着要进入主页,我需要键入类似http://localhost/public/v1/page
。所以我需要去http://localhost/public/v1
因此,我将上述代码更改为如下内容:
Route::group(array('prefix'=>'v1'), function(){
Route::resource('/', 'PageController', ['only'=>['index','show']]);
});
这仅适用于http://localhost/public/v1
,如果我导航到类似http://localhost/public/v1/our-products
它将产生错误,表示找不到路线。是否有解决方法(不使用.htaccess重写)
此外,如果可能的话,我想剥离链接中的v1
,但是api代码仍然存在于路由中,是否可能(同样,没有htaccess)?谢谢你的帮助
编辑
这是我的PageController
:
<?php
class PageController extends MediaController { //MediaController extends BaseController
protected $layout = 'layouts.master';
private $data = array();
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index() //this one for index/home page
{
$data = array();
$data['data'] = null;
$css_files = $this->addCSS(array('styles'));
$plugin_files = $this->addJqueryPlugin(array('unslider'));
$data['css_files'] = $this->addCSS(array('styles'));
if(!empty($plugin_files) && isset($plugin_files)) {
$data['css_plugin'] = $plugin_files['css_files'];
$data['js_plugin'] = $plugin_files['js_files'];
}
$data['js_files'] = $this->addJS(array('app'));
$data['title'] = 'Homepage - PT Anugerah Bhandala Sejati';
$this->layout->content = View::make('page.home', $data);
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id) //this one for OTHER THAN home page (like news page or product page, so for example, the link "http://localhost/public/v1/our-products" logic will go here)
{
if($id === "our-products") {
$data = array();
$data['data'] = null;
$css_files = $this->addCSS(array('styles'));
$plugin_files = $this->addJqueryPlugin(array('unslider'));
$data['css_files'] = $this->addCSS(array('styles'));
if(!empty($plugin_files) && isset($plugin_files)) {
$data['css_plugin'] = $plugin_files['css_files'];
$data['js_plugin'] = $plugin_files['js_files'];
}
$data['js_files'] = $this->addJS(array('app'));
$data['title'] = 'Our Products - PT Anugerah Bhandala Sejati';
$this->layout->content = View::make('page.product', $data);
}
else
return "Page not found";
}
}
来自您的评论:
“您在上面看到的是当前我的所有路线,我正在尝试制作/作为主页,/我们的产品作为产品类别页,我们的产品/项目1作为产品特定页,/新闻作为所有新闻页,/新闻/新闻1/新闻标题作为新闻特定页”
我的理解是这样的
在你的app/routes.php上
Route::resource('our-services', 'ProductsController', ['only'=>['index','show']]);
Route::get('news/{cat}/{title}', 'NewsController@single'); //news single post
Route::get('news', 'NewsController@index'); //news listings page
Route::get('/', 'HomePageController@index'); //home page
你只需要先了解你想要什么,然后一步一步地去做
请注意,这不会像现在这样起作用,但我希望这会让事情变得更清楚
更新
如果仍要将它们包装到v1中,则:
Route::group(array('prefix'=>'v1'), function(){
Route::resource('our-services', 'ProductsController', ['only'=>['index','show']]);
Route::get('news/{cat}/{title}', 'NewsController@single'); //news single post
Route::get('news', 'NewsController@index'); //news listings page
Route::get('/', 'HomePageController@index'); //home page
});
更新2
从你的另一个评论中,我得出以下结论:
Route::resource('v1', 'PageController', ['only'=>['index','show']]);
所以你有一个http://localhost/public/v1
和ahttp://localhost/public/v1/{resourceid}
然后自动生成的路由..您能列出您想要的路由吗?我不确定我是否理解你的意图。@Manuel:你在上面看到的是我目前所有的路线,我试图将/
作为主页,/our products
作为产品类别页面,our products/item1
作为产品特定页面,/news
作为所有新闻页面,/news/news1/title of the news
作为特定于新闻的页面,以及稍后ajax请求的其他路由(此处没有会员资格),您能否在问题上发布所需的路由/URL,并在此处显示您的routes.php
文件?我不确定,但这一定是路由的顺序。为什么要添加数组('prefix'=>'v1')
部分呢?类似于Route::resource('v1','PageController',['only'=>['index','show']]代码>所以你有一个http://localhost/public/v1
和ahttp://localhost/public/v1/{resourceid}
路由自动生成是的,我意识到使用Route::get
我可以做到这一点,我只想知道如果没有所有这些Route::get
(正如您在我的编辑中看到的,show
方法将处理除/
路由以外的其他路由)使用将意味着您将自动添加两个资源路由。如果您希望自定义路由不在该列表中,请将其放在route::resource
声明之前,因为它可能有冲突(此处需要更多信息)。。