How to make every Laravel URL have parameter - php

So I want to create admin page,
The result I want is like this
foo.com/admin/anthony
foo.com/admin/anthony/dologin
foo.com/admin/anthony/index
I tried using this routes
Route::any('admin/{username}', 'adminController#login'):
Route::any('admin/{username}/login', 'adminController#dologin');
I want to use every username parameter in admin page, but wont redeclare every page.

you can create group of route in laravel, below is the example which might help you.
Route::group(['prefix' => 'admin/{username}'], function () {
Route::get('login', 'adminController#dologin');
});
Let me know if this doesn't work for you.

Related

Trying to put in the url with laravel

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 ! :)

Detect if action is show or edit in Laravel 5.2

I have a middleware that detects if a user owns a tournament.
So, if user want to edit a tournament he doesn't own, he will get a 403.
Thing is I can't make difference between laravel.dev/tournament/1/edit, and laravel.devl/tournament/1
Off course, I could check the "edit" word in URL, but I would prefer other better param...
I tried method param in Request Object, but it is giving me GET for both, so I can't make difference...
Any Idea???
In your case, you can do like this:
$request->route()->getName();
Now you can do your logic based on this.
What about using a different HTTP method for edit, e.g PATCH or PUT and declaring two different routes, something like:
Route::get('laravel.devl/tournament/1', 'TournamentController#showTournament');
Route::put('laravel.dev/tournament/1/edit', 'TournamentController#editTournament');
Then in the TournamentController you can check if the user has rights to edit.
It sounds like you should just use route specific middleware instead of global middleware. See https://laravel.com/docs/master/middleware#assigning-middleware-to-routes. Then you can just do:
Route::get('/tournament/{id}/edit', ['middleware' => ['tournamentOwner'], function () {
//
}]);
Route::get('/tournament/{id}', ['middleware' => [], function () {
//
}]);
If it's a RESTful route, you can just do:
public function __construct()
{
$this->middleware('tournamentOwner', ['only' => ['edit']]);
}

Create user profile page laravel

I was looking on the internet to do this, but I did not find nothing. I want to make this:
I have a table with users, I want to create a single page for every user with his information. That's not hard. But, what I'm trying to figure it out is the following. I want to get into this page with the following username localhost/user/username (username is the username of the user). The thing is how can I get the username from the URL and How can I do the routing for that? Provide me the documentation or something.
Thank you in advance.
PS: I am here right now
Route::get('pilot/info/{license}', function($license)
{
return 'User '.$license;
});
My question here is how can I go to the controller sending the $license variable?
As going to the controller I am referring to doing that. But I also need to put the $license variable.
Route::get('/pilot/login', array('uses' => 'PilotController#getLogin', 'as' => 'getLogin'));
As you've done here:
Route::get('pilot/info/{license}', function($license)
{
return 'User '.$license;
});
Your controller will work in the same way.
Route::get('pilot/info/{license}', 'Controller#getLicense');
Will pass the {license} to the method as an argument. So all you need to do is add a $license parameter in your getLicense method and you can use it.

Laravel sub-domain routing without passing variable to URL::route()

In a Laravel 4 app, i'm using subdomain routing around a bunch of Route::resource's like this:
Route::group(['domain' => '{account}.my.app'], function()
{
Route::group(['before' => 'auth'], function($account)
{
Route::resource('organisations', 'OrganisationsController');
Route::resource('clients', 'ClientsController');
Route::resource('domains', 'DomainsController');
});
});
In my auth filter i'm doing the following:
Route::filter('auth', function($route)
{
// you could now access $account in a controller if it was passed as an argument to the method
$account = $route->getParameter('account');
// share account variable with all views
View::share('account', $account);
// Auth::guest Returns true if the current user is not logged in (a guest).
if (Auth::guest()) return Redirect::guest('login');
});
Within my views I can now access $account, but if I want a call to URL::route() to be correct I have to manually pass the account variable, like URL::route('clients.show',['account' => $account]) otherwise it generates URLs like %7Baccount%7D.my.app.
This is a bit of a pain and doesn't seem that elegant, is there any other or better way to achieve this? I guess I could create my own route helper to use instead of the built-in one.
However, I also do redirects with Redirect::route() within controllers so I would also need to make updates here.
EDIT
As suggested in the comments it may be that extending the Route API is the best approach here. Does anyone have any suggestions how this should be done?
Thanks.

Laravel 4 Resource Routes and Authentication

I'm not sure of the terminology I should be using so please bear with me, hopefully if I haven't got this right, someone might be able to tease the right question out!
ok.. so I have
Route::resource('gameworlds', 'GameworldsController');
This is fine. There are views for create, edit, index and show as you would expect and they all work fine. What I would like to do is only allow access to the "create" part when a user is logged in.
For example.. I have another route in my routes.php file:
Route::get('dashboard', array('before' => 'auth', function()
return View::make('dashboard/index');
}));
This works as expected, but I don't really understand how I can put similar code in the resource route for the "create" part only. Can someone explain that part to me please?
Many thanks.
DS
Well you don't need a filter, but instead you can use the Auth check method to check if user is logged in or not:
if (Auth::check()) { //Logged in }
In your controller method to make sure the user is logged in, and if he isn't you can do a redirect, like:
return Redirect::to('user/login');
However if you want to use a filter you could use the beforeFilter method in the __construct of your controller, like this:
public function __construct()
{
$this->beforeFilter('auth', array('on' => array('create')));
}

Categories