Laravel custom urls saved in database - php

I want to give custom urls to members of my site to have their pages under my site url like www.mysite.com/{theirurls}.
In database in user table i have a field userurl that keeps the url i give them.
In route i have written the code
Route::get('/{fpage?}', 'AController#fofpage');
In controller in fofpage function i take the variable and look for the value in database. If it belongs to someone redirects to this. If it isnt goes back to / (root).
The problem is that all the other urls like login and others doesnt work.
How can i solve this problem?
Thanks in advance

Put this route at the end of the web routes file to make it work:
Route::get('/{fpage?}', 'AController#fofpage');
You'll also want to validate user URLs and not allow users to enter values like login, register etc.

Related

PHP Rewrite URL or Mask or Redirect?

I'm trying to get domain.com/employee?display_county=sd&dept=sales&id=23 to display in the URL as domain.com/firstnamelastname while retaining the URL variables.
And also if someone goes directly to domain.com/firstnamelastname it would still have the original URL variables.
How would one accomplish this?
Edit: OK, so I'm starting to rethink this...
Let's say I have a page that displays a list of users.
Each user has a clickable link that directs to domain.com/employee?display_county=XX&dept=XXXXX&id=XX
How could I get the URL to display domain.com/firstnamelastname when a user is clicked and still bring up the correct user?
I was thinking maybe using a POST method to pass the id to the script that displays the user and then rewriting the URL to display the firstnamelastname related to the id, but then browsing directly to domain.com/firstnamelastname would leave the id variable empty...

Redirect to full url, using id

I am working on a webapp. There are items accessible the way using item id in the url web.site/123, where 123 is item id. I want to redirect visitors to the full url page like web.site/123-the-item, and I also want to use full links within my website.
Any good idea how to do it?
I don't need the code, I am looking for a simple yet nice and effective idea. The title is obviously in the database, so there will be one select needed anyhow. But I am not sure, if I should redirect once and every single time visitor visits the site without checking if the url is full (doesn't seem nice) or should I check the url and redirect only if it doesn't match the title?
I am using nginx, php, mysql and laravel 5.2 in case there is a workaround for this scenario I am not aware of.
Try adding route like
Route::get('/{id}',['uses' => 'ContentController#byId'],'where'=>['id'=>'\d*']);
And in that action you can check in database if id exists and redirect user to proper url
You can check use regular expressions to check the if the incoming request has just the id and have them mapped to particular routes. In the digit route you can redirect.
E.g.
your route file should be something like this :
Route::get('item/{id}', function ($id) {
//redirect to the full name route
})
->where('id', '[0-9]+');
Route::get('item', 'SomeController#somemethod');
Read more here.

Laravel 4 change route based on current URL

I have a login form spread across my website on different pages for different situations that are all used in the same POST controller. In the POST controller I always redirect->back() so it goes to the previous page the visitor was on. However, now I need it setup differently. I need it to know which page they came from and based on that previous page know which page to redirect the user to.
So basically I need a Route::getCurrentRoute()->getPath() for the previous page. So it can be something like this:
Route::getCurrentRoute()->getPath() == "some/url" then go to some/url/2
If I actually use getCurrentRoute() it just shows my POST route and not the route the visitor is coming from.

How to create new webpages automatically based on user input?

This question may have been asked before but I couldn;t really find it.
What I want to do is, websites like pastebin.com, even stackoverflow, these generate new webpages based on user input showing that data from what I can understand.
I want to make it like that. User enters something, and he is given a permalink to share that information.
How to do this using PHP ?
EDIT: Here is an example
Like, I want it that
instead of having something like www.example.com/view.php?id=123
I want it like
www.example.com/view/123
This is impossible to be done with PHP, you can do this with .htaccess's url rewriting.
a good article about this can be found at: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
In this site , work some ajax function, when you add some data in textarea and click submit, called function , that saved current data in db, and return some json data , and then show you in some format.
Simply done like this:
Create a index.php containing a input field where the user inputs their data.
Post data to the server and create a random string which will be the users permalink.
Then insert the random string and data into a database.
In the index.php file say that if any uri is added after your domain like example.com/
it checks against the database if this code exists in db, and returns the result to the visitor.

.htaccess converting id to user via mySQL database

I'm looking to use mod_rewrite to mask user profiles on my site. However, their profiles are decided by their id in the format /profile.php?id=1. Both 'user' and 'id' are in the mySQL table 'users'. Is there a way to reroute the URL to read /user? Sorry if that's badly explained!
RewriteEngine on
RewriteRule ^profile/([^/\.]+)/?$ /profile.php?user=$1 [L]
This will cause all /profile/username to be redirected to profile.php?user=username. You can then check in your PHP:
if (isset($_GET['user'])) {
// Check if the username exists.
// SELECT ... FROM users WHERE username = ..
}
.htaccess will not be able to use the info from the database to do what you desire. Even if it could you shouldn't do it :).
But what you can do is to change the profile.php to be able to read an user by the name. Let's say to make profile.php able to process a new parameter name= and you add logic in it to look in the database either via an id or via the name. If you have this then is actually trivial to route / to /profile.php?name=.
You will also have to change the website generation to output proper urls.
Also look at Cristian's answer for more technical details. It's the same idea in the end.
You wouldnt change this in htaccess, or with any kind of config/rewrite rule. This would be done in your application PHP if I understand what you want.
in /profile.php?id=1 profile.php is (rewrite rules aside) the source PHP file, and id is a GET parameter. Its like an argument to profile.php when it executed. They come in the form source.php?get1=val1?get2=val2.... and you access them through the array $_GET
If you don't want users to see that id, you can do one of two things.
Replace it with user, as you suggest. URL would be 'profile.php?username=XXX'
Then in your code you will presumably need to perform a simple database query to get the id from the user name. Should be about 5 lines towards the top of the code in profile.php and the rest can remain unchanged Beware that this demands unique user names in case you werent before, and some special characters might be a problem with get parameters in the URL? For most user names, uniqueness is required and special characters are not allowed, so hopefully this isnt a problem.
Keep id but make it a POST peremeters. URL would be 'profile.php'
POST parameters are similar to get but they are not visible to the user because they aren part of the URL string. Just switch $_GET to $_POST in profile.php. The code will still identify users by id, but they won't be able to see their IDs.
In either situation you will need to change any incoming links.actions that lead to the descrubed URL that we are changing.
EDIT - Christian's has a good suggestion which is similar to #1 above but would allow for nicer looking URLs like profile/XXX rather than 'profile.php?username=XXX'. Code within profile.php would ne hte same, his changes are in profile.php are the same

Categories