How to hide function and parameter from url in codeigniter? - codeigniter

this is my URL.
http://localhost/savyy/home/home_page/Islamabad
i want to show it like
http://localhost/savyy/home
i m using uri routing code
$route['home/(:any)'] = 'home/home_page/$1;
but nothings happen...it shows same URL. can anyone please help me ? please tell me how to hide function name and parameter.

I dont think it will work that way, the request is the route to controller. the request uri will be the same. you will still have the "home/home_page/islamabad" in your url. unless you redirect with the home_page() to home() and save the islamabad in a session.

Related

CodeIgniter URL Encryption

All Viewers I am New in Codeigniter, I need your guide to done my work, I want to Encrypted full URL like below example.
For example this is my url www.example.com & my controller is home, so full url is www.example.com/home
now I want to encrypted all controller, function like below
www.example.com/5115784bef2514430e7f74d9a71d4142a942efb0f7cc428626bda7633326f9d015fbacc60d93cd6b858f9b6e05c1e56263acb24297cecc720467eb4f222d81e5hdn5B
I can encrypted & decrypted the text well, but I just don't get how can I decrypted from url & make understand which controller or function its called, I want to decrypted everything after base_url.
please don't suggest me about using common controller, because I already know that & anyhow common controller its hide everything so its not required the encryption as I believe.
Waiting for your positive response, hopefully my problem will be solve soon. T.I.A
Well i never encrypt any URL before but you can use a php function url_encode
And "str_replace" function.
the reason for using "str_replace" beacause url_encode only encode special character in URL.
Hope I help some.
Try the code below.
urlencode(str_replace("your_domain.com/YourCOntrollerName/YourMethodName" , "SM5ah52" , yor_domain.com . "YourCOntrollerName/YourMethodName/YOuData"));
If not this. There is an library in CI Framework called Encryption.
You can get help from there Encryption.
Go with URI Routing and define one controller to decode whatever you are passing, and call proper controller / method from it.
You can use URI Routing with regular expressions.
$route["other_controllers/method"] = "other_controllers/method"; //you can add this kind of lines to not to affect other controllers
$route["([a-zA-Z0-9]+)"] = "home/decrypt/$1";
In the home controller, You can
Redirect to the page
Or
Load a view
public function decrypt($token){
//geting the page according to the token from database.
$desired_page = $this->some_model->get_page($token);
//if you want to redirect
redirect($desired_page);
//if you want to load a view
$this->load->view($desired_page);
}

Laravel 5.7 Passing a value to a route in a controller

My controller posts a form to create a new page. After posting the form I need to redirect the user to the new page that will have the contents for that page that were entered in the previous form. If I simply do return view('mynewpageview', compact('mycontent')); where my mycontent is the object used to execute the $mycontent->save(); command, I carry the risk for someone refreshing the url thus posting the same content twice by creating a new page.
Instead I would like to redirect the user to the actual page url.
My route is
Route::get('/newpage/{id}', 'PageController#pagebyid'); and if I use return redirect()->route('/newpage/$pageid'); where $pageid = $mycontent->id; I get Route not defined error.
What would be the solution either to stop someone from resubmitting the content or a correct syntax for passing the parameter?
The correct answer that works for me is -
Give your route a name in the routes file
Then pass the parameters with an array as shown below in the controller.
return redirect()->route('newpageid', ['id' => $pageid]);
With basic (unnamed) routes, the correct syntax was return redirect('/newpage/'.$pageid);
You have already found out you can alternatively use named routes.
Last but not least, thanks for having considered the "double submit" issue! You have actually implemented the PRG pattern :)

how to get full url with # in laravel

I am using laravel 5 and I am trying to get my full url which is
/faq#manual
However I can only get /faq
when I call the function
Request::url() or Request::fullUrl()
is there any way I can get the full url
with #manual?
It is impossible to get the fragment from a URL with Standard HTTP. So using variables like $_SERVER or $_GET can't help you. Actually, the server never knows about this value because the browser won't even send a request with a fragment part.
If you want to have the fragment value in the server side, you should get the value by javascript first then pass it as a URL parameter and get it by $_GET in your server side.
That's not possible in http or php however you can use javascript to get the full url including the hash.
var url = document.URL;
or
var url = window.location.href;
Hope this helps.

CodeIgniter routes are not working

I've defined following route in config file as follows.
$route['apartments/(:any)'] = 'apartments/view/$1';
If I give http://localhost/apartment_advertisement/apartments/shobha_complex like this in url it works perfectly fine.
If I give http://localhost/apartment_advertisement/apartments/shobha_complex/abcd/abcd like this in url it goes to the same page as above. So I needed error page for this url. Please help me how to control these urls?. The work would be more appreciated.
Do you mean display an 404-not-found error when request URL has an unwanted "tail"? You can modify (:any) to restrict accepted string. It's simple:
$route['apartments/(\w+)'] = 'apartments/view/$1';

user profile routes configuration in codeigniter

I have a simple problem but I just can't figure it out. I wrote a method that reads parameters from the url (one parameter which is the username) and queries all data to that user and displays it in the page.
the only problem is that the method is part of a controller and naturally it has to show in the url (which is simply said: not too nice for sharing. Also I have to write the string as follows:
www.domain.com/controller/profile_guest?user=username
I want to get rid of all that is before username. So for it to show as:
www.domain.com/username
Now there are two scenarios to cover here.
a) someone browse the catalogue, clicks on profile name and then redirects to profile page. once there he likes what he sees, he copies url, and share it.
b) someone receives the shared link and clicks on it, so what should load is the profile page with the url he received not the full one showing controller and method.
is this possible to achieve?
Thanks for the help :)
Update:
$route['default_controller'] = "main/index";
$route['404_override'] = '';
you can use this route:
$route['(:any)'] = "controller/profile_guest/$1";
but any other route, you will need to write it manually above this route.
for example:
$route['controller/view'] = "controller/view";
$route['(:any)'] = "controller/profile_guest/$1";
Explain:
The routes are handled in the order in which they appear on your routes.php file, so if you put $route['(:any)'] at the top, it will handle anything.
suppose that you have a contoller and a function inside it, and you need to execute it for example: www.domain.com/contoller/function, you cann't execute your function because it will match this route $route['(:any)'], so you need to define a route for it before $route['(:any)']
for example:
$route['contoller/function'] = "controller/function"
$route['(:any)'] = "controller/profile_guest/$1";
and do the same thing with all your contollers and its function, but you have to put $route['(:any)'] at the last route.

Resources