Create user profile page laravel - php

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.

Related

Making sub category in laravel website

so i want to expand my site to have like region site, so when user open my site it will be ask to select which region he/she interested into and lets just say he interesting in London, then the site will generate a webpage that already all content related to that region...
i tried to just do the same way i create administrator page by group them in this route
Route::group(array('prefix'=>'admins','before' => 'auth'),function(){
Route::resource('partner','AdminPartnerController',array('except' => array('show')));
Route::get('partner/index_kategori/{id}',array(
'as' => 'admins.partner.index_kategori',
'uses' => 'AdminPartnerController#index_kategori'
});
but it's mean that i need to create a different controller function for each region i gonna create, so it's not very efficient.
what comes to my mind is just like making
www.websiteadress.com/region
so how i can catch that "/region" and add that value into each my function in controller? how to build that kind of routes?
update:
to make it more easily to understand, what become my problem is like i have this normal route:
Route::get('product/{id}',array( 'as' => 'product','uses' => 'PublicController#product'));
what this route do is pretty straight foward, when i type
www.websiteadress.com/product/12
it will open the product page for number 12, so my problem is if i add like this
www.websiteadress.com/Asia/product/12
how i make my PublicController to catch Asia and later in my product function i will just process that..?
Of course you don't need to create controller for each region. You want to use simple route with a variable:
Route::get('region/{region}', 'RegionController#show');
And just one controller for all regions:
public function show ($region)
{
// Logic here
return view('region.show', compact(['region']));
}
So, when user will load http://example.com/region/London, show action will have London in $region variable.
so after searching for days and asking around, i finally getting the best solution to answer my own question and i willing to share it to here, so someday maybe there is other people who want to do what want to...
so first in the route just create like this
Route::group(['prefix' => '{region}'], function(){
Route::get('member',array('as' => 'member','uses' => 'PublicController#member'));
});
and what this route do is just group it into prefix with value is in region variable and then i set the get route to where the controller will use to show the page and also taking region value into use
and then in my PublicController
public function sitemap($region){
if($region =='london'){
//do something
}else{
//do something
}
}
and so there it is.. i can get those region value from prefix and use it. in my case i use it to show certain page with different content.

How to make every Laravel URL have parameter

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.

Intercepting and changing route-based input in Laravel?

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'
]);

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')));
}

CakePHP 1.2: How do I lock a given controller from access from anyone but admins?

I'm not looking for the whole ACO-ARO implementation... I just want to use Auth, and check against the user's role....
What do I put where in order to simply deny users from a given controller unless they have a certain role.
I'm trying to use the $this->Auth->authorize = 'controller';
... but I don't even know where to put that??
Any help would be awesome!
Thanks in advance.
Short answer: Sounds like you need to create and app_controller.php and put your code in the beforeFilter method.`
Longer Answer: Create an app_controller.php file in you app directory and put the following code in beforeFilter().
if (isset($this->params[Configure::read('Routing.admin')])) { //User is trying to access a page using the admin route
if ($this->Session->check('someSessionVariable')) { //Check user has some session variable set.
// User is accessing an admin page and has permission, do something, or in most cases do nothing.
} else { //No sessions set for user, redirect to login page.
$this->redirect('/yourLoginPage'); //Redirect
}
}
This is no substitution for proper user of the Auth component, but should do what you need. Make sure you check its secure before you put it into production.

Categories