如何在laravel中读取URL参数

wmvff8tz  于 2023-11-20  发布在  其他
关注(0)|答案(5)|浏览(116)

我是乞丐,并试图创建一个转介系统。以下是样本网址

http://dev.lea.com/register?ref=mh2HPLpVSn

字符串
我尝试获得引用字符串。我如何单独获得?是否需要中间件来获得Cookie?如果是,那么中间件的代码是什么?以及我如何在RegisterController中获得引用引用?
这是我的注册控制器。

<?php

namespace Lea\Http\Controllers\Auth;

use DB;
use Lea\User;
use Lea\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Cookie;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'fname' => 'required|string|max:255',
            'lname' => 'required|string|max:255',
            'referral' => 'string|max:255',
            'username' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            // 'tracking_id' => 'required',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \Lea\User
     */
    protected function create(array $data, Request $request)
    {

        //  printing here referral Link
        echo $request()->ref;

        // $referred_by = Cookie::get();
        // print_r($referred_by); 
        die;

        return User::create([
            'fname' => $data['fname'],
            'lname' => $data['lname'],
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'affiliate_id' => str_random(10),
            // 'referred_by' => $referred_by,
            'identity' => $data['identity'],
            'dob' => $data['dob'],
            'board_id' => $data['board_id'],
            'class_id' => $data['class_id'],
            'subject_id' => $data['subject_id'],
            'institute' => $data['institute'],
            'address' => $data['address'],
            'city' => $data['city'],
            'std_mobile' => $data['std_mobile'],
            'father_name' => $data['father_name'],
            'father_mobile' => $data['father_mob'],
            'ipadress' => \Request::ip(),
        ]);
    }

}

3duebb1j

3duebb1j1#

如果你有一个URI,比如/register?ref=mh2HPLpVSn,你可以像这样检索ref

$request()->ref

字符串
我相信你也可以使用$request()->has('ref')来确定它是否存在于URI中。
希望这对你有帮助!

gxwragnw

gxwragnw2#

在laravel 6+里面的控制器你可以使用像

$request->get('ResourcePath')

字符串

y0u0uwnf

y0u0uwnf3#

您可以使用Request access获取参数。
这里有一个获取ref代码的例子:

public function register(Request $request)
{
    $allParams = $request->all();
    $refParam = $request->ref;
    dd($allParams, $refParam);

    ...
}

字符串

hs1rzwqc

hs1rzwqc4#

我们也可以使用下面的格式,

public function handle(Request $request)
    {
        $reference = $request->query('ref');
    
    }

字符串

xxslljrj

xxslljrj5#

public function showRegister(Request $request) {
    var_dump(request->ref)
}

字符串
然后你可以做一些像验证之类的事情,另一种可能性是改变你的register表单的模板,并添加一个隐藏字段,其中包含ref参数中的refcode,并在register方法中处理整个请求。

相关问题