How do I add username to url?
Example: www.domain.com/feeds/username
I want to add username to every route url.
::Route::
Route::get('feeds', array('before' => 'auth', 'as' => 'feeds', 'uses' => 'FeedsController#getFeedsPage'));
::Controller::
class FeedsController extends BaseController {
public function getFeedsPage() {
return View::make('feeds.index');
}
}
Thanks in advance.
Michael.
You may try this, declare the route like this (Notice {username}):
Route::get('feeds/{username}', array('before' => 'auth', 'as' => 'feeds', 'uses' => 'FeedsController#getFeedsPage'));
Declare the class:
class FeedsController extends BaseController {
// Use a parameter to receive the username
public function getFeedsPage($username) {
// You may access the username passed via route
// inside this method using $username variable
return View::make('feeds.index')->with('username', $username);
}
}
Now you may use a URI like these:
www.domain.com/feeds/username1
www.domain.com/feeds/michaelsangma
Related
So i have been working with Laravel and Auth0 for some time now and i think i am at the end :)
I am able to log into my application using a link to the widget / hosted page
Now everything seems to be working and once the page callbacks to my site the user I saved in my database.
However, it doesn't seem that my application remembers the user.
When i attempt to check if a user is logged in:
$isLoggedIn = \Auth::check();
it says false
I have tried debugging multiple functions however with no results so i am kind of lost on where to start with this.
Does anyone know why this is happening?
My configuration
So this is my AuthController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class AuthController extends Controller
{
public function __construct()
{
}
public function login()
{
return \App::make('auth0')->login(null, null, ['scope' => 'openid profile email'], 'code');
}
public function logout()
{
\Auth::logout();
return \Redirect::intended('/');
}
public function dump()
{
$isLoggedIn = \Auth::check();
return view('dump')
->with('isLoggedIn', $isLoggedIn)
->with('user',\Auth::user()->getUserInfo())
->with('accessToken',\Auth::user()->getAuthPassword());
}
}
Then in my web:
Route::get('/login', ['as' => 'login', 'uses' => 'AuthController#login']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'AuthController#logout'])->middleware('auth');
Route::get('/dump', ['as' => 'dump', 'uses' => 'AuthController#dump', 'middleware' => 'auth'])->middleware('auth');
Route::get('/auth0/callback', '\Auth0\Login\Auth0Controller#callback');
I am having trouble with Laravel routes. I'm trying to redirect to a controller after some middleware in the routes. But there is always this error.
The error is:
InvalidArgumentException in UrlGenerator.php line 558: Action
App\Http\Controllers\DashboardController#index not defined.
The route code is:
Route::get('/dashboard', ['middleware' => 'auth', function() {
return Redirect::action('DashboardController#index', array('user' => \Auth::user()));
}]);
The controller:
class DashboardController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return view('dashboard')->with('user', \Auth::user());
}
}
But the above code actually works (so I guess that the controller actually works):
Route::get('/testdashboard', [
'uses' => 'DashboardController#index'
]);
So what is the problem? What is a valid route action?
This is might a better way to do it, change from
Route::get('/dashboard', ['middleware' => 'auth', function() {
return Redirect::action('DashboardController#index',
array('user' => \Auth::user()));
}]);
to
Route::get('/', [
'middleware' => 'auth',
'uses' => 'DashboardController#index'
]);
This is rather a comment than a post, but I can't send it at this time. I don't undestand why do you pass parameter (\Auth:user()) to a method that doesn't require it (but it's correct when you do it for the View).
Anyways I suggest you to work on your Middleware
public function handle($request, Closure $next)
{
if (Auth::check()) {
return redirect(...);
} else {
return redirect(...);
}
}
Use this route in place of your route and upgrade your Laravel Project to Laravel 8:
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
Route::get('/dashboard', 'DashboardController#index')->name('daskboard');
});
I am trying to run the code from a book "Laravel blueprints", chapter 4: "Personal blog" on Laragon and Bitnami Laravel stacks.
I put all the controllers, views, models and routes in respective app folder, set up database so it shows connection and tables migrated, however, when I want to run home page, it says: "Post class not found". I can`t return the home view of the blog. Please help: what I have missed in configurations, how to run examples from book on Laragon and Bitnami Laravel, what files to copy, what remain unchanged and what to configure?
Thanks
<?php
class PostsController extends BaseController{
public function getIndex()
{
$posts = Posts::with('Author')->orderBy('id', 'DESC')->paginate(5);
return View::make('index')
->with('posts',$posts);
}
public function getAdmin()
{
return View::make('addpost');
}
public function postAdd()
{
Posts::create(array(
'title' => Input::get('title'),
'content' => Input::get('content'),
'author_id' => Auth::user()->id
));
return Redirect::route('index');
}
}
-
class Post extends Eloquent {
//the variable that sets the table name
protected $table = 'posts';
//the variable that sets which columns can be edited
protected $fillable = array('title','content','author_id');
public $timestamps = true;
public function Author(){
return $this->belongsTo('User','author_id');
}
}
<?php
/*
|--------------------------------------------------------------------------
Route::get('/', array('as' => 'index', 'uses' =>
'PostsController#getIndex')); Route::get('/admin', array('as' =>
'admin_area', 'uses' => 'PostsController#getAdmin'));
Route::post('/add', array('as' => 'add_new_post', 'uses' =>
'PostsController#postAdd')); Route::post('/login', array('as' =>
'login', 'uses' => 'UsersController#postLogin'));
Route::get('/logout', array('as' => 'logout', 'uses' =>
'UsersController#getLogout'));
Looks like a namespacing error, I had struggle with it at the beggining too.
Please show us you Post class code, your routes.php file and the controller that is throwing the error.
Actually before doing this, change all places in script where you use Post:: for \App\Post::
I'm building a Laravel 4 app and I want to protect my admin area so it is only accessible if the user is logged in/authenticated.
What is the best way to do this?
The Laravel docs say you can protect a route like this:
Route::get('profile', array('before' => 'auth', function()
{
// Only authenticated users may enter...
}));
But what happens when my route looks like this:
Route::resource('cms', 'PostsController');
How do I protect a route that is directing to a controller?
Thanks in advance!
You could use Route Groups for this purpose.
So for example:
Route::group(array('before' => 'auth'), function()
{
Route::get('profile', function()
{
// Has Auth Filter
});
Route::resource('cms', 'PostsController');
// You can use Route::resource togehter with
// direct routes to the Resource Controller
// so e.g. Route::post('cms', 'PostsController#save');
});
You can put the filter on the constructor of your Controller like this:
public function __construct()
{
$this->beforeFilter('auth');
$this->beforeFilter('csrf', array('on' => 'post'));
$this->afterFilter('log', array('only' =>
array('fooAction', 'barAction')));
}
In your PostsController you can put a closure in the constructor to do the same before logic as the previous route.
public function __construct()
{
$this->beforeFilter(function()
{
//
});
}
Having the following controller:
class Admin_Images_Controller extends Admin_Controller
{
public $restful = true;
public function __construct()
{
parent::__construct();
}
public function get_index($id)
{
echo $id;
}
I don't understand why when I access it with no parameter for ID it works, as I get an error says missing parameter for ... but when I actually try to pass a parameters at http://site/admin/images/12 I get a 404 error. What am I missing?
I tried setting the following in my routes, no success either:
Route::any('admin/images', array(
'as' => 'admin_images',
'uses' => 'admin.images#index',
));
//or
Route::any('admin/images/(:any)', array(
'as' => 'admin_images',
'uses' => 'admin.images#index',
));
It seams that my issues with wildcards, 90% happen in my test linux envirnonment (ubuntu). Here's my routes.php that I'm currently using http://pastebin.com/f86A3Usx
it could be that you're using the same alias (admin_images) and also, check your order - put the more specific ones first, and more generic as you go down, like so:
Route::any('admin/images/(:any?)', array('uses' => 'admin.images#index'));
Have removed the alias, just for readability.
Route::get('admin/images/(:any)', 'admin.images#index');
You should make the $id parameter optional, by passing a default value (like null/false/1)
public function get_index($id = null)
{
if($id){
echo $id;
}else{
echo "No ID given!";
}
}
And use (:any?) in your route.
Updated Routes:
Route::any('admin/images/(:any?)', array(
'as' => 'admin_images',
'uses' => 'admin.images#index',
));
You can simplify your routing by combining your routes for each endpoint. By adding the "?" into your first parameter, this mean that anything can be present, but doesn't have to be. So both /admin/images and /admin/images/1234 are covered.
Updated Controller:
class Admin_Images_Controller extends Admin_Controller
{
public $restful = true;
public function __construct()
{
parent::__construct();
}
public function get_index($id=null)
{
echo $id;
}
// ...
}
With the addition of "= null" into your method parameter, you can now handle both routes into this function. A simple check of "equals null" within your method should put you well on your way to covering each senario.