dongqi9125
dongqi9125
2015-12-11 18:58

URL id等于if语句或elseif语句打开页面

已采纳

1- Work correct in this step

this url work

This is correct work enter image description here

2- Work not in this step

help me in this step

index.blade.php page

navbar code

 <nav class="navigation">
          <ul class="sf-menu">
            <li><a href="{{ url('/') }}">Home</a>
            </li>
            <li><a href="{{ url('about') }}">About Us</a></li>
            <li><a href="javascript:;">Category</a>
              <ul class="dropdown">
                <li><a href="{{ url('grid/1') }}">Buy and Sell</a></li>
                <li><a href="agent-detail.html">Car and Vehicles</a></li>
                <li><a href="my-properties.html">Real Estate</a></li>
                <li><a href="submit.html">Pets</a></li>
                <li><a href="pricing.html">Jobs</a></li>
                <li><a href="login.html">Community</a></li>
                <li><a href="404.html">Resumes</a></li>
              </ul>
            </li>
  1. problem in this anchor tag (1) pass to URL enter image description here

My Route code

Route::resource('/', 'BasicController');
Route::resource('about', 'BasicController@about');
Route::resource('contact', 'BasicController@contact');
Route::resource('grid', 'BasicController@grid');

This is my controller code

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class BasicController extends Controller
{
    public function index(){  
        return view('index');
    }
    public function about(){   
       return view('about');
    }

    public function contact(){
       return view('contact');
    }
     public function grid($id)
{  
    $id = $this->uri->segment(3);

    if ($id == 1) {
       return view('buy-and-sell/grid');
    }
    elseif ($id == 2) {
        return view('car-and-vehicels/grid');
    }
    elseif ($id == 3) {
        return view('country/grid');
    }
    else{
         return view('other/grid');
    }

  }
}

click on buy and sell enter image description here

To Face this problem

  1. First problem enter image description here

    BasicController

    public function grid($id) {

    $id = $this->uri->segment(3); <!-- Line number 30-->
    
    if ($id == 1) {
       return view('buy-and-sell/grid');
    }
    elseif ($id == 2) {
       return view('car-and-vehicels/grid');
    }
    elseif ($id == 3) {
       return view('country/grid');
    }
    else{
       return view('other/grid');
    } 
    

    }

  2. second problem enter image description here

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doulangxun7769 doulangxun7769 6年前

    You have to apply the following changes:

    1.) Your method signature is wrong. See below, Laravel will pass in the "number" automatically from your url in the variable $id (but as second(!) argument). Be ware you also have to import the Request in the header via an use Illuminate\Http\Request; statement!

    public function grid(Request $request, $id)
    

    2.) Dont use $this->uri->segment but rather use the passed $id directly (this is the slug that represents segement 3). You are using resource Controllers and Laravel does the mapping automatically for you.

    So it should look likes this:

    public function grid(Request $request, $id)
        {  
            //$id = $this->uri->segment(3); //COMMENT THIS OUT!!!
    
            if ($id == 1) {
               return view('buy-and-sell/grid');
            }
            elseif ($id == 2) {
                return view('car-and-vehicels/grid');
            }
            elseif ($id == 3) {
                return view('country/grid');
            }
            else{
                 return view('other/grid');
            }
    

    3.) Regarding the CSS. You got your paths wrong, but this is extremely hard to debug/help without further information

    Important: Its seems you are running laravel as a subfolder install. You will run into very strange problems (like this one, I suspecte this the subfolder install could be the reason). I have opened a bug at laravel some time ago, but it got closed. Subfolder Installs are not supported.

    I really advise you to install laravel as described on their homepage as a dedicated virtual host, that the public folder maps to the domain itself (like http://localhost) in other words "public" is your root folder that is served directly under http:://localhost.

    点赞 评论 复制链接分享