How to encrypt GET url for search in Laravel 5.5? - php

I am getting stuck on this problem, I don't know hot to encrypt the URL for search in Laravel 5.5... the result like this :
localhost:8000/Akademik/Mahasiswa?cari=some_keyword
but I want like this :
localhost:8000/Akademik/Mahasiswa?cari=some_encrypted_keyword
like :
localhost:8000/Akademik/Mahasiswa?cari=Kas6F8ajhasdhhfbdgshek
this my MahasiswaController.php
public function index(Request $request)
{
if ($request->get('cari') == null) {
$datas = Mahasiswa::paginate(10);
return view('Akademik.Mahasiswa.mahasiswaIndex', compact('datas'))->with('no',($request->input('page',1)- 1)*10);
} else {
$cari = $request->get('cari');
$datas = Mahasiswa::where('nama','LIKE','%'.$cari.'%')->paginate(10);
return view('Akademik.Mahasiswa.mahasiswaIndex', compact('datas'))->with('no',($request->input('page',1)- 1)*10);
}
}
This my route/web.php
Route::Resource('Akademik/Mahasiswa','Akademik\Mahasiswa\MahasiswaController');
and This my mahasiswaIndex.blade.php (search form)
<div class="col s4 m6 right">
{{ Form::open(array('url' => 'Akademik/Mahasiswa','method' => 'get')) }}
<div class="row">
<div class="input-field col s12">
{{ Form::text('cari',null,['id' => 'cari','class' => 'col s12']) }}
<label for="cari">Cari</label>
</div>
</div>
{{ Form::close() }}
</div>

if you want to encrypt your input field. you must do in javascript/jquery AJAX before Sending the keyword to url result. let assume you have controller and route to make encryption like this :
localhost:8000/Akademik/encrypt
after that, you will get the keyword encrypted on var some_encrypted_keyword then send again via Ajax GET to url :
localhost:8000/Akademik/Mahasiswa?cari=some_encrypted_keyword
ask me anything. hope this is solve your problem

Are you sure it is actually what you need? For security considerations HTTPS protocol is used, which encrypts all the communication between client side and the server. If you simply want to hide raw data from your browser address bar, you might you POST method instead of GET.

You can encrypt your url parameter and decrypt it in your controller. You can try this:
In your view: Suppose your parameter is cari or more parameter you can encrypt.
<?php
$parameter =[
'cari' => (value of input field),
];
$parameter= Crypt::encrypt($parameter);
?>
a link
Your route will be:
Route::get('/url/{parameter}', 'YourController#methodName');
In your controller, You can decrypt your parameter:
public function methodName($cari){
$data = Crypt::decrypt($cari);
}
You must be use Crypt namespace in your top of controller as
use Illuminate\Support\Facades\Crypt;
Note: You can encrypt url parameter with Crypt::encrypt($parameter) and decrypt with Crypt::decrypt($parameter)

Related

How can I get id from url with Request $request? (Laravel 5.3)

My link href on the view is like this :
<a href="{{ route('message.inbox.detail.id', ['id' => $message->id]) }}" class="btn btn-default">
<span class="fa fa-eye"></span> {{ trans('button.view') }}
</a>
My routes is like this :
Route::get('message/inbox/detail/id/{id}', ['as'=>'message.inbox.detail.id','uses'=>'MessageController#detail']);
When clicked, the url display like this :
http://myshop.dev/message/inbox/detail/id/58cadfba607a1d2ac4000254
I want get id with Request $request
On the controller, I try like this :
public function detail(Request $request)
{
dd($request->input('id'));
}
The result : null
How can I solve it?
You can get it like this
$request->route('id')
Inside request class you can access it this way
$this->route('id')
I usually use it when I'm validating field to make sure it's unique, and I need to exclude model by ID when I'm doing update
This is because you're passing the id parameter through url, you should send it using a form like this
{!! Form::open(['route'=>' route('message.inbox.detail.id', 'method'=> 'POST']) !!}
<input type="hidden" name="id" value="{{ $message->id }}" />
{!! Form::submit({{ trans('button.view') }}, ['class'=>'btn btn-default']) !!}
{!! Form::close() !!}
and now you can access via request in your controller
public function detail(Request $request)
{
dd($request->input('id'));
}
For people who are looking for answers using newer versions of laravel, you can get the route id using the following:
Example route with model binding
Route::put('/post/{post}, [PostController::class, 'update]);
$this->route('id') will not work in this case due to model binding.
instead you can do this:
// using in controller
request()->route('post.id);
or
// using in form request
$this->route('post.id');
Get the id via this:
public function detail($id)
{
dd($id);
}

laravel - controller passing data to view (use return redirect to )

below is my route code
Route::any("rgMain/SystemManage/Account" , function()
{
$c1 = "rg.SystemManage.AccountManage.Account.Account_Factor";
$c2 = "rg.SystemManage.AccountManage.Account.Account_Result";
return View::make("rgMain",array(
'rgFactor' => $c1 ,
'rgContent' => $c2
));
this is my controller code:
return Redirect::to('rgMain/SystemManage/Account')->with('message2', $output);
when do something done in webpage Account , submit param to controller ,
after vaild , controller would return Redirect::to route ,and carry message2
back to Account , but i could'nt work
I am not sure this problem is or not about my structure
this my main struct , it could load different use webpage in here
<div>
<div>
</div>
<div>
#include('rg.rgNotice')
</div>
<div>
#include('rg.rgTitle')
</div>
<div>
#include('rg.rgMenu', ['rg_Menu' => 'default'])
</div>
<div>
#include($rgFactor, ['rg_Factor' => 'default'])
</div>
<div>
#include($rgContent, ['rg_Result' => 'default'])
</div>
in rgMain.blade.php , try use input.get('message2'); and $message2 , but
still couldn't work .
When using with() with a Redirect, it actually flashes the data to the session so rather than trying to pull it from the input, try Session::get('message2')
Please note that this data would only be available during this request.
Redirects using with flashes data to session
You are able to retrieve the data with:
{{ old('message2') }}

Passing form values with RESTful routing in Laravel 4

I have setup nested RESTful resource routes like so:
Route::group(array('prefix'=>'opening-hours'), function(){
Route::resource('library', 'LibraryController');
Route::resource('library.interval', 'LibraryIntervalController');
});
I have a blade form which has a select dropdown with options populated from the db, like this:
{{ Form::open(array('route' => 'opening-hours.library.show', 'method' => 'GET')) }}
<legend>Select a library to edit</legend>
<div class="form-group">
<label for="">Please select a library to modify its opening hours:</label>
<select class="form-control" name="id" required>
#foreach ($library_options as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
{{ Form::close() }}
The form submits to the named route:
opening-hours.library.show
The routing itself works but I have a couple of questions about the logistics (I'm a little confused about how to use the routing):
I can get the form to submit to the correct route if I make it a
'GET' request (GET/HEAD opening-hours/library/{library} =>
opening-hours.library.show), as the corresponding POST route does not
exist
how do I pass the user-selected $id to the controller show($id)
method?
when I submit the form, it goes to the URI
"../opening-hours/library/%7Blibrary%7D?id=3". So if the user selects
the library with id=3, how do I make the URI "../opening-hours/library/3"?
Here is my Library controller show method:
/**
* Display the specifed library.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
return "This is a library with id: " . $id . "!";
}
When I submit the form, the following is displayed:
This is a library with id: {library}!
Of course, I'd want it to display:
This is a library with id: 3!
I'm obviously not understanding something critical about how REST or Laravel or both work here. Any pointers would be very much appreciated, I've spent a day knocking about with this!
Thanks a lot
Try using
$id = Input::get('id');
Documentation
I would also recommend using the provided helpers for forms:
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'));
Documentation
Writing out the problem seemed to help me come up with the solution. It seemed silly to use a form to send a 'GET' request, so I thought (bing!) why not just construct the URIs as anchor tag href attributes. So instead of using a form, I did:
#foreach( $library_options as $id => $name )
<a class="btn btn-primary" href="{{ URL::to('opening-hours/library/' . $id) }}">Edit {{ $name }}</a>
#endforeach
That solved the issue of dodgy URIs and also the id was directly available from the RESTful controller method like so:
public function show($id)
{
return "This is a library with id: " . $id . "!";
}

passing parameter in laravel

i have an index pages with a link_to:
<h1> User:</h1>
#foreach ($user as $user)
{{link_to("/users/{$user->username}",$user->username)}}
#endforeach
then i have a route:
Route::get('/users/{{$username}}', function($username){
return View::make('show')->with('username',$username);
});
Now, if i understand clear, i am passing username as parameter to function, and username is my url, now if i pass parameter to my show view,
<body>
<div>
<h1> User:</h1>
{{$username}}
</div>
</body>
</html>
I should be able to see it in my page. Where i am wrong? I can't take a parameter from the url when i use get?
Why i need to do:
Route::get('/users/{{$username}}', function($username){
$user=User::whereUsername($username)->fist();
return View::make('show')->with('username',$user);
});
Your route is wrong, this is the correct one:
Route::get('/users/{username}', function($username){
$user=User::whereUsername($username)->fist();
return View::make('show')->with('user',$user);
});
It's just
/users/{username}
and not
/users/{{$username}}
Also, you view will receive an user object, so you have to:
<div>
<h1> User:</h1>
{{$user->username}}
</div>
EDIT
In Laravel there are 2 kind {}:
1) In views you have to use {{}} or {{{}}} (escaped version). Inside them you put PHP code:
{{$variable}}
{{ isset($variable) ? $variable : 'default value' }}
2) In routes, you just use {} and inside it it's not PHP, just a route parameter name, without $:
/user/{name}
/user/{id?} (in this case id is optional, might or might not be send)

Can you add a class/id to a session in laravel?

I'm creating a website with laravel and when a user edits their details it will access the controller update them and redirect to the 'edit' page using this
return Redirect::to('/member/editprofile')->with('message', 'Information Changed');
this part works fine, it redirects and sends the message which is printed out on the page with this
{{ Session::get('message') }}
I was wondering if there was a way to add a class to the session? I'm probably missing something completely obvious here and this is what I tried...
{{ Session::get('message', array("class" => "success")) }}
//added the class as you would with a HTML::link
any help is appreciated, thanks in advance!
You want this
<div class="success">{{ Session::get('message') }}</div>
In Laravel-4, the Session::get accepts two arguments :
$value = Session::get('key', 'default');
$value = Session::get('key', function() { return 'default'; });

Categories