What I am looking to achieve here is to have the username set as myapp.com/username. I can achieve this by doing:
/*
* Other route logic, containing static-url pages, such as
* myapp.com/login so that it overrides any usernames
*/
Route::get('/{username}', function($username) {
return $username . "'s profile.";
});
However, I also want to include company pages following the same rule. For example, myapp.com/janes-bakery. But I cannot seem to achieve this because Laravel automatically stops searching for routes if it comes across one that returns nothing. Instead it will just throw a 404 page.
I did think that I could use something similar along these lines:
Route::get('/{slug}', ['as' => 'profile', function($slug) {
$company = \App\Models\Company::where('slug', '=', $slug)->get();
if($company->count() > 0) {
return "Company found.";
}
$user = \App\User::where('username', '=', $slug)->get();
if($user->count() > 0) {
return "User found.";
}
abort(404);
}]);
Which works fine, however I feel as though it's bad practice. Besides that, I cannot access suffix routes based on the company/user. For example myapp.com/jane/friends or myapp.com/janes-bakery/services.
Does anybody have any recommendations on how to go about this? I am finding it very hard to think of any solutions. Thank you in advance!
P.S. It is important the URLs have no correlation, e.g. no myapp.com/company/user (or vice versa), myapp.com/company_name_here and myapp.com/users_name_here are two totally separate things.
You can stick with your route and make another route with company/user pattern. You can do it this way.
Create another route with company and user parameter.
Route::get('{company}/{user}', function($company, $user){
dd($company, $user);
});
And you need to put this route above your profile route because, in laravel, the first route present will be processed before the others routes below.
Related
So I'm trying to build this application where users have their usernames in the domain name ( domain.com/hisusername for example) and this is what I did, so this is my route
Route::group(['prefix' => '/{username}'], function($username){
Route::get('/', 'UserController#UserProfile');
});
And my controller
public function UserProfile($username){
$user = User::where('username', $username)->first();
if (!$user) {
abort(404);
}
return view('pages.profile')
->with('user', $user);
}
It works fine the problem is when I try to add another route it gets confused with a username and it returns a 404 page, how can I fix that please?
Laravel renders routes from top to bottom. Often when I encounter this problem it's because I need to put my 'catch-all' routes below all the others.
That being said, I would strongly suggest doing something like domain.com/u/user instead to avoid conflicts with existing pages.
It may seem like a stretch, but if you ever had a someone with the username 'login' they might never be able to access their account.
Try like this
Route::group(['prefix' => 'user'], function($username){
Route::get('/{username}', 'UserController#UserProfile');
Route::get('/otherinfo', 'UserController#otherinfo');
});
Route::get('/more/other/route', 'OtherController#methodinfo');
in your browser it will display like. sample.com/user/yourUsername
Okay it's seem very easy :
Route::get('/{username}', ['uses' => 'UserController#UserProfile']);
I don't know why you have to use the group prefix? You just need to define a get routes as normal, you controller look good, it should work ! :)
So I've been using Laravel a lot lately, and it's great! But I've found myself banging my head against the keyboard on this issue I'm having.
I have this pattern:
Route::pattern('id', '(\d*|(me))');
And this route is required for a lot of my API calls. What it's supposed to do, is give consumers the option to simply append /me at the end of the call, to get info relating to them, so not having to use the userId. I can of course put this login in the controller, no problem, but I would love to be able to put some login in the "pattern", meaning that if this pattern is used, that I can check what userId "/me" correspons to and translate it. I want to do it this way to avoid having to write the same code translating "/me" in all controllers where this is used.
Hope someone has a clever solution out there! :-)
You can try using a Route filter. Something like this ought to work:
Route::filter('route_filter_name', 'F\Q\ClassName');
<?php namespace F\Q;
class ClassName
{
/**
* #param Illuminate\Routing\Route
*/
public function filter($route)
{
$userId = $route->getParameter('id');
if($userId == 'me' && ($user = Auth::user())) {
$route->setParameter('id', $user->id);
}
}
}
This is more specific to the route rather than your route pattern as it depends on what name you give the id parameter in each route that you want to use it in. An example route that uses it would look like this:
Route::get('/users/{id}', [
'before' => 'route_filter_name',
'uses' => 'UserController#showUserInfo'
]);
With laravel we have a few auths setup for us which we can control in the routes file or setup in the controller constructor however I need to find a better way of doing this, and not sure if a route can handle it?
With my users, I allow themselves and the admin to edit them. So my controller looks like this.
function edit($id)
{
$user = User::findOrFail($id);
if(!Auth::user()->isAdmin() && $user->id != Auth::user()->id)
{
return Redirect::route('users.index')->withError('Unable to access that user');
}
return View::make('users.edit', compact('user'));
}
which is ok, but then on my update code I also have to do the same auth/user check to make sure a user or admin can only make changes to themselves.
So we get a double up, in some controllers this is repeated 3 times. Another example of context would be a forum post, the person who posted it or the admin can edit it.
Is there a way with route filters to handle doing this?
This may or may not be of use to you, I have a similar problem in my application where a user can only access their own user profile, but management can access anyone's.
My routes look like:
Route::get('user_profile/{user_id?}', array('as' => 'user_profile', 'uses' => 'UserController#get_user_profile'))->where(array('user_id' => '[0-9]+', 'tab' => '[A-Za-z]+'))->before('auth');
Route::when('user_profile/*', 'management');
This applies a management filter if the user attempts to go to a specific user, it defaults to their own profile if no ID is given.
Route::filter('management', function()
{
if(Helper::managementStatus() === NOT_MANAGEMENT)
return Redirect::route('home')
->with('flash_error', 'You must be logged in as a manager to view this page!');
});
Alternatively you could create a filter something like:
Route::filter('management', function()
{
$user = User::findOrFail(Input::get('user_id'));
if(!Auth::user()->isAdmin() && $user->id != Auth::user()->id)
{
return Redirect::route('users.index')->withError('Unable to access that user');
}
}
And then just apply that filter to the relevant routes.
I hope that's helpful.
I have these filters in place
Route::when('app/*', 'auth');
Route::when('app/*', 'filled_basic_info');
The "filled_basic_info" filter redirects the user to the 'users.personal' route if the basic information of the user is not yet filled.
The 'users.personal' route leads to the "app/personal" url.
My question is, how do I set the 'filled_basic_info' filter to be applied on all the routes that lead to 'app/*' except the 'users.personal' route?
I have tried
Route::when('app/*', 'filled_basic_info', array('except' => 'users.personal'));
but it doesn't work.
I'd love to avoid grouping all the 'app/*' routes as I would need to do that on all the packages I have in place.
Thank you.
I ended up editing the filter like so:
Route::filter('filled_basic_info', function()
{
if (empty(Auth::user()->first_name) || empty(Auth::user()->last_name))
{
if(!(Route::currentRouteName() == "user.personal"))
return Redirect::route('user.personal')->withAlerts(['Please enter your name below to continue.']);
}
});
There will be several high profile links for customers to focus on, for example:
Contact Us # domain.com/home/contact
About the Service # domain.com/home/service
Pricing # domain.com/home/pricing
How It Works # domain.com/home/how_it_works
Stuff like that. I would like to hide the home controller from the URL so the customer only sees /contact/, not /home/contact/. Same with /pricing/ not /home/pricing/
I know I can setup a controller or a route for each special page, but they will look the same except for content I want to pull from the database, and I would rather keep my code DRY.
I setup the following routes:
Route::get('/about_us', 'home#about_us');
Route::get('/featured_locations', 'home#featured_locations');
Which work well, but I am afraid of SEO trouble if I have duplicate content on the link with the controller in the URL. ( I don't plan on using both, but I have been known to do dumber things.)
So then made routes like these:
Route::get('/about_us', 'home#about_us');
Route::get('/home/about_us', function()
{
return Redirect::to('/about_us', 301);
});
Route::get('/featured_locations', 'home#featured_locations');
Route::get('/home/featured_locations', function()
{
return Redirect::to('/featured_locations', 301);
});
And now I have a redirect. It feels dumb, but it appears to be working the way I want. If I load the page at my shorter URL, it loads my content. If I try to visit the longer URL I get redirected.
It is only for about 8 or 9 special links, so I can easily manage the routes, but I feel there must be a smart way to do it.
Is this even an PHP problem, or is this an .htaccess / web.config problem?
What hell have I created with this redirection scheme. How do smart people do it? I have been searching for two hours but I cannot find a term to describe what I am doing.
Is there something built into laravel 4 that handles this?
UPDATE:
Here is my attempt to implement one of the answers. This is NOT working and I don't know what I am doing wrong.
application/routes.php
Route::controller('home');
Route::controller('Home_Controller', '/');
(you can see the edit history if you really want to look at some broken code)
And now domain.com/AboutYou and domain.com/aboutUs are returning 404. But the domain.com/home/AboutYou and domain.com/home/aboutUs are still returning as they should.
FINAL EDIT
I copied an idea from the PongoCMS routes.php (which is based on Laravel 3) and I see they used filters to get any URI segment and try to create a CMS page.
See my answer below using route filters. This new way doesn't require that I register every special route (good) but does give up redirects to the canonical (bad)
Put this in routes.php:
Route::controller('HomeController', '/');
This is telling you HomeController to route to the root of the website. Then, from your HomeController you can access any of the functions from there. Just make sure you prefix it with the correct verb. And keep in mind that laravel follows PSR-0 and PSR-1 standards, so methods are camelCased. So you'll have something like:
domain.com/aboutUs
In the HomeController:
<?php
class HomeController extends BaseController
{
public function getAboutUs()
{
return View::make('home.aboutus');
}
}
I used routes.php and filters to do it. I copied the idea from the nice looking PongoCMS
https://github.com/redbaron76/PongoCMS-Laravel-cms-bundle/blob/master/routes.php
application/routes.php
// automatically route all the items in the home controller
Route::controller('home');
// this is my last route, so it is a catch all. filter it
Route::get('(.*)', array('as' => 'layouts.locations', 'before' => 'checkWithHome', function() {}));
Route::filter('checkWithHome', function()
{
// if the view isn't a route already, then see if it is a view on the
// home controller. If not, then 404
$response = Controller::call('home#' . URI::segment(1));
if ( ! $response )
{
//didn't find it
return Response::error('404');
}
else
{
return $response;
}
});
They main problem I see is that the filter basically loads all the successful pages twice. I didn't see a method in the documentation that would detect if a page exists. I could probably write a library to do it.
Of course, with this final version, if I did find something I can just dump it on the page and stop processing the route. This way I only load all the resources once.
applicaiton/controllers/home.php
public function get_aboutUs()
{
$this->view_data['page_title'] = 'About Us';
$this->view_data['page_content'] = 'About Us';
$this->layout->nest('content', 'home.simplepage', $this->view_data);
}
public function get_featured_locations()
{
$this->view_data['page_title'] = 'Featured Locations';
$this->view_data['page_content'] = 'Featured properties shown here in a pretty row';
$this->layout->nest('content', 'home.simplepage', $this->view_data);
}
public function get_AboutYou()
{
//works when I return a view as use a layout
return View::make('home.index');
}