Author: Shiannizhilu public number: Zhixing Research Institute

1. The framework provides a URL () helper function to generate any desired URL; (You can also use urls)

// Generate the specified URL
$user = User::find(19);
return url('/user/'.$user->id);
Copy the code

The execution result is:

http:/ / 127.0.0.1:8000 / user / 101
Copy the code

2. If url() does not give an argument, more methods can be executed as objects;

If the access URL is: http://127.0.0.1:8000/user/index? name=ZhangSan&id=10001&age=1024

Perform the following operations:

// Get the current URL, with no parameters
return url()->current();	/ / with: URL: : current (); 【 need: use Illuminate\Support\Facades\URL】;
// Get the current URL, with parameters
return url()->full();		/ / with: URL: : full ();
// Get the last URL
return url()->previous(); 	/ / with: URL: : previous ();
Copy the code

The results are as follows:

http:/ / 127.0.0.1:8000 / user/index

http:/ / 127.0.0.1:8000 / user/index? age=1024&id=10001&name=ZhangSan

// The third result can be simulated
Copy the code

3, use the route() method to generate the URL of the named route

// Create a route
Route::any('/url/{id}'.'UserController@url')->name('url.id');
// Execute in any method:
return route('url.id'['id'= >5]);
Copy the code

The result is:

http:/ / 127.0.0.1:8000 / url / 5
Copy the code

4. You can use the controller directly or return the URL.

// Use the controller to return the URL
return action('UserController@url'['id'= >5.'name'= >'ZhangSan']);
Copy the code

Results:

http:/ / 127.0.0.1:8000 / url / 5? name=ZhangSan
Copy the code

4. Generate a signature URL and append a hash signature string to the URL for verification;

return url()->signedRoute('url.id'['id'= >5]);
Copy the code

Execution result:

http://127.0.0.1:8000/url/5?signature=c34b75b164b80959641cf6702186d6e3e2a5504e4c03c82722b00c7d092cff04
Copy the code

In the URL /{id} route, we can verify the hash signature:

return request()->hasValidSignature();
Copy the code

If correct, return 1.

The above.