Laravel点击链接会添加到网址

时间:2017-06-06 13:32:39

标签: laravel laravel-5 blade laravel-blade

好的我有一个小问题。

当用户点击链接时,它会转到网站/创建/业务,这很好但是如果没有做任何事情但是再次点击该链接,则无论出于何种原因它都会转到website / create / business / create / business。

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
        {{ Auth::user()->name }} <span class="caret"></span>
    </a>
    <ul class="dropdown-menu" role="menu">
        <li>
            <a href="profile/{{Auth::user()->id}}">Profile</a>
            <a href="create/business/">Add Business</a>
            <a href="{{ url('/logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
                Logout
            </a>
            <form id="logout-form" <a href="{{ url('home') }}" action="{{ url('/logout') }}" method="POST" style="display: none;">
                {{ csrf_field() }}
            </form>
        </li>
    </ul>
</li>

路线:

Route::get('profile/{user_id}', 'ProfileController@checkid');
Route::post('update', 'ProfileController@updateProfile');
Route::get('create/business', 'BusinessController@addBusiness');
Route::post('create', 'BusinessController@createBusiness');

ProfileController可:

public function checkid($user_id) {
    if (Auth::check())
    {
        $user_id = Auth::id();
        return view('profile', [
            'id' => $user_id
        ]);
    }
}
function updateProfile(Request $request) {
    $user = $request->user();
    $twitter = $request->input('twitter');
    $facebook = $request->input('facebook');
    $instagram = $request->input('instagram');
    $telephone = $request->input('telephone');
    $user->twitter_personal = $twitter;
    $user->facebook_personal = $facebook;
    $user->instagram_personal = $instagram;
    $user->telephone = $telephone;
    $result = $user->save();
    if($result) {
        $message = 'success';
    }else{
        $message = 'error';
    }
   return redirect()->back()->withInput()->with('message', $message);
}

BusinessController:

function addBusiness() {
    return view('addBusiness');
}

function createBusiness(Request $request) {
    $name = $request->input('name');
    $type = $request->input('type');
    $email = $request->input('email');
    $user_id = Auth::id();
    $business = new Business();
    $business->name = $name;
    $business->type = $type;
    $business->email = $email;
    $business->user_id = $user_id;
    $business->save();
    $address1 = $request->input('address1');
    $address2 = $request->input('address2');
    $town = $request->input('town');
    $city = $request->input('city');
    $postcode = $request->input('postcode');
    $telephone = $request->input('telephone');
    $address = new Address();
    $address->firstline_address = $address1;
    $address->secondline_address = $address2;
    $address->town = $town;
    $address->city = $city;
    $address->postcode = $postcode;
    $address->telephone = $telephone;
    $address->save();
    $result = $business->save();
    $result2 = $address->save();

    $business_id = $business->id;
    $address_id = $address->id;
    DB::table('business_address')->insert(array('business_id' =>  $business_id, 'address_id' => $address_id));
    DB::table('user_business')->insert(array('user_id' => $user_id, 'business_id' => $business_id));

    if($result && $result2) {
        $message = 'success';
    }else{
        $message = 'error';
    }
    return redirect()->back()->withInput()->with('message', $message);
}

2 个答案:

答案 0 :(得分:3)

<a href="create/business/">必须<a href="/create/business/">才能解决此问题,因为您当前的链接是相对的,而不是绝对的,因此当您再次单击它时,会在当前网址的末尾添加相同的引用。 / p>

无论如何,您应该以Laravel方式生成链接以避免将来出现其他问题:

Route::get('create/business', 'BusinessController@addBusiness');

<a href="{{ url('create/business') }}">

Route::get('create/business', 'BusinessController@addBusiness')->name('createBusiness');

<a href="{{ route('createBusiness') }}">

我个人更喜欢第二个,所以如果我更改路线网址,链接仍然可以使用,但需要在路线定义中添加name('yourRoute')

答案 1 :(得分:0)

尝试使用//Register handlers and it's sub router func RegisterHandlers(r *mux.Router) { //base handler, i.e. /v1 r.StrictSlash(true) r.HandleFunc("/", BaseHandler) //example sub-router, i.e. /v1/example ex := r.PathPrefix("/example").Subrouter() ex.HandleFunc("/", ExampleHandler) //other handlers... } 功能。对于此示例:route()。当您的路线发生变化时,它会在整个应用程序中自动更改。

您可以这样命名您的路线:

<a href="{{ route('my_route_name') }}">Add Business</a>