2014-10-16 153 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>