2014-10-16 146 views
3

我对laravel相当陌生,而且我正努力使我的url格式正确。在laravel中通过url传递变量

它格式,

http://mysite/blog?category1 instead of http://mysite/blog/category1 

这是我使用的文件,有没有把路线进入BlogController

Route.php

Route::get('blog/{category}', function($category = null) 
{ 
    // get all the blog stuff from database 
    // if a category was passed, use that 
    // if no category, get all posts 
    if ($category) 
     $posts = Post::where('category', '=', $category)->get(); 
    else 
     $posts = Post::all(); 

    // show the view with blog posts (app/views/blog.blade.php) 
    return View::make('blog.index') 
     ->with('posts', $posts); 
}); 

Blogcontroller

方式
class BlogController extends BaseController { 


    public function index() 
    { 
     // get the posts from the database by asking the Active Record for "all" 
     $posts = Post::all(); 

     // and create a view which we return - note dot syntax to go into folder 
     return View::make('blog.index', array('posts' => $posts)); 
    } 
} 

blog.index刀片

@foreach ($posts as $post) 

    <h2>{{ $post->id }}</h2> 
    <p>{{ $post->name }}</p> 
    <p>{{ $post->category }}</p> 
    <h2>{{ HTML::link(
    action('[email protected]',array($post->category)), 
    $post->category)}} 


@endforeach 
+0

你对Apache或nginx的,我觉得这是URL重写问题。 – 2014-10-16 14:00:06

+0

“它的格式为”是什么意思?当你输入浏览器时?或者Laravel生成的链接? – 2014-10-16 14:32:36

+0

laravel从db生成的链接。它现在显示为http:// localhost/blog?category = category1,它也不会过滤数据库结果,所以某处出错了。 – 2014-10-16 15:01:12

回答

0

而不是使用功能的回调为您Route::get使用一个控制器和一个动作:

Route::get('blog/{category}', 'BlogControlle[email protected]'); 

现在您的BlogController您可以创建功能。

class BlogController extends BaseController { 

    public function index() 
    { 
     // get the posts from the database by asking the Active Record for "all" 
     $posts = Post::all(); 

     // and create a view which we return - note dot syntax to go into folder 
     return View::make('blog.index', array('posts' => $posts)); 
    } 

    /** 
    * Your new function. 
    */ 
    public function getCategory($category = null) 
    { 
     // get all the blog stuff from database 
     // if a category was passed, use that 
     // if no category, get all posts 
     if ($category) 
      $posts = Post::where('category', '=', $category)->get(); 
     else 
      $posts = Post::all(); 

     // show the view with blog posts (app/views/blog.blade.php) 
     return View::make('blog.index') 
      ->with('posts', $posts); 
    } 
} 

更新:

要在视图中显示你的链接,你应该使用HTML::linkAction而不是HTML::link

@foreach ($posts as $post) 

    <h2>{{ $post->id }}</h2> 
    <p>{{ $post->name }}</p> 
    <p>{{ $post->category }}</p> 
    {{ HTML::linkAction('[email protected]', "Linkname", array('category' => $post->category)) }} 

@endforeach 
+0

谢谢,我已经更新了我的代码,但它仍然显示链接?而不是/。 – 2014-10-16 14:17:23

+0

谢谢,我已经更新了我的代码与上述,它仍然显示链接为http:// mysite/blog?category1而不是http:// mysite/blog/category1 – 2014-10-16 14:18:16

+0

在我的应用程序中我的地址?category = 1然后在我的控制器中,我提取了category = Input :: get('category')的变量; – Peter 2016-01-02 10:18:19

0

有如图所示,你试图使用可替代的.htaccess文档? 在这里你去:

Options +FollowSymLinks 
RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [L] 

你需要把它放在你的应用程序的文件夹public

这里是万一原来的.htaccess你没有它无论出于何种原因

<IfModule mod_rewrite.c> 
<IfModule mod_negotiation.c> 
    Options -MultiViews 
</IfModule> 

RewriteEngine On 

# Redirect Trailing Slashes... 
RewriteRule ^(.*)/$ /$1 [L,R=301] 

# Handle Front Controller... 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [L] 
</IfModule> 
+0

我刚刚尝试过这种方式,没有运气,它仍然以同样的方式显示 – 2014-10-16 15:20:17

+0

您可以尝试在“.htaccess”中输入一些垃圾值以验证它是否有效?如果它工作,你应该得到一个'500内部服务器错误' – Adrenaxus 2014-10-16 15:28:57

+0

是的,它刚刚收到服务器错误 – 2014-10-16 15:29:53

0

我加入了一个新的途径:

Route::get('blog/{category}', ['as' => 'post.path', 'uses' => '[email protected]']); 

,并添加新的链接进入index.blade:

<a href="{{ URL::route('post.path', [$post->category]) }}">{{ $post->category }}</a> 
5

routes.php

Route::get('category', '[email protected]'); 

* .blade.php打印完成的URL

<a href="{{url('category/'.$category->id.'/subcategory')}}" class="btn btn-primary" >Ver más</a>