【问题标题】:Passing a variable through url in laravel在laravel中通过url传递变量
【发布时间】:2014-12-11 22:21:52
【问题描述】:

我对 laravel 还很陌生,我正在努力让我的网址格式正确。

它的格式为http://mysite/blog?category1 而不是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);
});

博客控制器

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('BlogController@index',array($post->category)),
    $post->category)}}


@endforeach

【问题讨论】:

  • 你是用apache还是nginx,我认为这是url的重写问题。
  • “它的格式为”是什么意思?当你在浏览器中输入?还是 Laravel 生成的链接?
  • laravel 从数据库生成的链接。它现在显示为localhost/blog?category=category1,并且它也不会过滤数据库结果,所以某处有问题

标签: php url laravel laravel-4 url-routing


【解决方案1】:

routes.php

Route::get('category', 'CategoryController@indexExternal');

*.blade.php 打印完成的url

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

【讨论】:

    【解决方案2】:

    我在以下位置添加了一条新路线:

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

    并在 index.blade:

    中添加了一个新链接
    <a href="{{ URL::route('post.path', [$post->category]) }}">{{ $post->category }}</a> 
    

    【讨论】:

      【解决方案3】:

      不要使用函数作为 Route::get 的回调,而是使用控制器和操作:

      Route::get('blog/{category}', 'BlogController@getCategory');
      

      现在您可以在您的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('BlogController@index', "Linkname", array('category' => $post->category)) }}
      
      @endforeach
      

      【讨论】:

      • 谢谢,我已经更新了我的代码,但它仍然显示带有 ?而不是 /.
      • 谢谢,我已经用上面的代码更新了我的代码,它仍然将链接显示为mysite/blog?category1而不是mysite/blog/category1
      • 在我的应用程序中,在我的 url 中放置 ?category=1,然后在我的控制器中,我使用 category = Input::get('category'); 提取变量;
      【解决方案4】:

      您是否尝试过使用文档中显示的替代 .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>
      

      【讨论】:

      • 我刚试过这个,但运气不好,它仍然以同样的方式显示
      • 您可以尝试在您的.htaccess 中输入一些垃圾值来验证它是否有效吗?如果它有效,你应该得到一个500 Internal Server Error
      • 好吧,至少 .htaccess 可以正常工作。我们需要更多信息,例如您使用的是 apache 还是 nginx,以及您正在使用什么服务器环境(LAMP、WAMP、...) - 还有:您的服务器上是否启用了 mod_rewrite?
      • mod_rewrite 已启用,我正在 xampp 上运行。为了清楚,如果我手动进入 mysite/blog/category1,它会显示正确的结果。但是它仍然使用 laravel HTML:Link 将 url 输出为 mysite/blog?category1
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 2016-02-02
      • 1970-01-01
      • 2011-11-03
      • 2012-09-30
      相关资源
      最近更新 更多