Laravel route pages with sidebar content - php

I've created a Laravel blog, the routing works in a way that every page has it's own Route::get('params').
This works fine and I can send the specific content to each page as needed.
The problem is that I'm trying to also send the sidebar content but I'm trying to prevent writing the same code over and over like this:
Route::get('/', function()
{
$sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
$posts = Post::orderBy('id', 'DESC')->get();
return View::make('index')->with('sidebarContent', $sidebarContent)
->with('posts', $posts);
});
Route::get('about', function()
{
$sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
return View::make('about')->with('sidebarContent', $sidebarContent);
});
What's the best way to do this? Is the following the way I should approach it?
Route::group(array(), function()
{
$sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
Route::get('/', function($sidebarContent)
{
$posts = Post::orderBy('id', 'DESC')->get();
return View::make('index')->with('posts', $posts)
->with('sidebarContent', $sidebarContent);
});
Route::get('about', function($sidebarContent)
{
return View::make('about')->with('sidebarContent', $sidebarContent);
});
});

I'll do something like this. :)
Route::get('/', array('uses' => 'Controller#index'));
Route::get('user', array('uses' => 'Controller#about'));
class Controller extends BaseController{
protected $sidebarContent;
public function __construct(){
$this->sideBarContent()
}
public function index(){
$posts = Post::orderBy('id', 'DESC')->get();
return View::make('index')->with('sidebarContent', $this->sidebarContent)
->with('posts', $posts);
}
public function about(){
return View::make('about')->with('sidebarContent', $this->sidebarContent);
}
public function sidebarContent(){
$this->sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
}
}

Related

Laravel 7 - Redirects to the main page when the optional parameter is empty

I have a route with an optional parameter in web.php:
my web.php: (the route that has the problem is marked with a comment)
Route::middleware(['auth', 'dashboard'])->group(function () {
Route::get('/', 'DashboardController#home')->name('root');
Route::prefix('/drivers')->group(function () {
Route::view('/', 'dashboard.driver.main');
Route::post('/', 'UserController#addDriver');
Route::get('/{id}', function ($id) {
if (Auth::user()->can('view_user')) {
$user = User::find($id);
return view('dashboard.user.view', ['user' => $user]);
}
return view('pages.403');
});
//----------------------------------------
// My route with the problem
// ---------------------------------------
Route::get('/driver-dropdown/{q?}', function ($q=null){
return $q;
})->name('driver.dropdown');
});
});
and it is my dashboard middleware:
public function handle($request, Closure $next)
{
if(!in_array(\Auth::user()->getOriginal('role'), ['superadmin', 'admin', 'supporter']) )
{
return abort(403);
}
return $next($request);
}
When I enter the host-name/drivers/driver-dropdown/jo URL, I get jo
BUT When I enter the host-name/drivers/driver-dropdown/ URL, I will be redirected to the host-name/ that means root route!
Edit: updated web.php
You should reorder your routes like this:
Route::get('/driver-dropdown/{q?}', function ($q=null){
return $q;
})->name('driver.dropdown');
Route::get('/{id}', function ($id) {
if (Auth::user()->can('view_user')) {
$user = User::find($id);
return view('dashboard.user.view', ['user' => $user]);
}
return view('pages.403');
});
Currently, when you go to host-name/drivers/driver-dropdown/, it will match the /{id} route.
If I’m correct the URL’s in your routes which are grouped shouldn’t start with a /, except for the main route of course. I’ve had this issue too.
Should be like this:
Route::middleware(['auth', 'dashboard'])->group(function () {
Route::get('/', 'DashboardController#home')->name('root');
Route::prefix('drivers')->group(function () {
Route::get('driver-dropdown/{q?}', function ($q=null){
return $q;
})->name('driver.dropdown');
});
});

Laravel 5.4 User Profile NotFoundHttpException

I am creating a user profile that allows him to modify his information here is the code
class ProfilesController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('content.profil');
}
public function editProfile($id)
{
$user = User::find($id);
return view('content.edit', ['user' => $user]);
}
public function updateProfile(Request $request, $id)
{
$user = User::find($id);
$user->name = $request->input('name');
$user->nom = $request->input('nom');
$user->prenom = $request->input('prenom');
$user->adresse = $request->input('adresse');
$user->code_postal = $request->input('code_postal');
$user->ville = $request->input('ville');
$user->pays = $request->input('pays');
$user->num_tele = $request->input('num_tele');
$user->save();
return redirect('/profil');
}
}
Web.php
Route::group(['middleware' =>'auth'], function(){
Route::get('/profil', 'ProfilesController#index')->name('profil');
Route::get('/content', 'ProfilesController#editProfile')->name('profil.edit');
Route::post('/content', 'ProfilesController#updateProfile')->name('profil.update');
});
the view folder tree looks like
view/content/profil.blade.php
view/content/edit.blade.php
the problem is that the routes are defined but it shows me this error message:
(1/1) NotFoundHttpException
I don't know where the problem exists exactly and
thanks in advance
Compared to your routes (web.php) and what you want, this is what your web.php file should be
Route::group(['middleware' =>'auth'], function(){
Route::get('/profil', 'ProfilesController#index')->name('profil');
Route::get('/content/{id}/editProfile', 'ProfilesController#editProfile')->name('profil.edit');
Route::post('/content/{id}', 'ProfilesController#updateProfile')->name('profil.update');
});
Correct your profil.edit route to /content/{id}/editProfile and profil.update in the same way.
And if you have named routes try to use route() helper instead of url() to generate url's, it's cleaner are more universal.

Laravel 5.4 Different routes pointing to different method but the response show the same page

I have an issue with my routes as some with different URLs and differents methods seems to use one method.
routes/web php :
Route::group(['middleware' => ['role:utilisateur,usage']], function()
{
Route::group(['prefix' => 'qcm'], function()
{
Route::get('/', 'ModuleController#index');
Route::get('{id?}', 'ModuleController#qcm');
Route::post('answer', 'ModuleController#putAnswer');
Route::get('result', 'ModuleController#getResult');
Route::get('get-question', 'ModuleController#getQuestion');
});
}
ModuleController.php
class ModuleController extends Controller
{
public function index()
{
return View::make('qcm.index')
}
public function qcm($id)
{
return View::make('qcm.qcm');
}
public function getQuestion()
{
return response()->json(['question' => 'test?']);
}
public function putAnswer(Request $request)
{
return response()->json(["result" => "next"], 200);
}
public function getResult()
{
return View::make('qcm.result');
}
}
When I call test.com/qcm/result or test.com/qcm/get-question, it seems to call test.com/qcm/{id} instead and returns its view and I don't know why as php artisan route:list shows that the routes are pointing to the good methods.
Does anyone knows why my code is producing such results ? Have I missed something ?
Thank you in advance for your help.
Put Route::get('{id?}', 'ModuleController#qcm'); to the end of your route list.
e.g:
Route::group(['middleware' => ['role:utilisateur,usage']], function()
{
Route::group(['prefix' => 'qcm'], function()
{
Route::get('/', 'ModuleController#index');
Route::post('answer', 'ModuleController#putAnswer');
Route::get('result', 'ModuleQCMController#getResult');
Route::get('get-question', 'ModuleController#getQuestion');
Route::get('{id?}', 'ModuleController#qcm');
});
}
Your
Route::get('result', 'ModuleQCMController#getResult');
Calls ModuleQCMController. Shouldn't it be calling ModuleController?
Like this :
Route::get('result', 'ModuleController#getResult');

Redirecting user to a specific page

I'm using Laravel 5.3 and Auth by default with this roles package. How can i do the normal user redirection after the user login if i have similar roles and also pages for them. For example i have AdminRole and after the Login i want to redirect user to /admin/dashboard.
I have tried something like this in the LoginController but it doesn't make sense:
protected function redirectTo()
{
if (Auth::user()->isRole('admin'))
return redirect()->route('admin');
return redirect()->route('home');
}
Or maybe there is a better way to use middleware for redirecting?
Here is my routes (web.php):
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
Route::resource('company', 'CompanyController');
Route::group(['prefix' => 'admin'], function () {
Route::get('login', function () {
return view('admin.pages.admin-login');
});
Route::group(['middleware' => 'role:admin'], function () {
Route::get('/', function () {
return view('admin.admin-main');
});
});
});
use it like this way:
return Redirect::to('admin');
And note that:
route:Route::get('company', 'CompanyController#show');
controller:
this works fine:
function show(){
return Redirect::to('home');
}
but this not
function show(){
$this->redirectto();
}
function redirectto()
{
return Redirect::to('home');
}
route.php
Route::get('home', ['as' => 'admin_home', 'uses' => 'HomeController#index']);
Route::get('login'['as'=>'admin_login','uses'=>'LoginController#admin_login']);
LoginController.php
use Illuminate\Support\Facades\Redirect;
public function index(){
$User=new User();
if(isset(AUTH::user()->id)){
$User->id=AUTH::user()->id;
$auth_user_role=$User->auth_user_role();
$rl_title=$auth_user_role[0]->rl_title;
if(isset(Auth::user()->id) && isset($rl_title) && $rl_title == 'Admin'){
return view('home.admin',$this->param);
}
else if(isset(Auth::user()->id) && isset($rl_title) && $rl_title == 'Moderator'){
return view('home.moderator',$this->param);
}
else{
return Redirect::route('admin_login');
}
}else{
return Redirect::route('admin_login');
}
}
Views
-> views
-> home
-> admin.blade.php
-> member.blade.php
I needed to do something like this in Auth/LoginController:
protected function authenticated()
{
if(Auth::user()->isRole('admin')) {
return redirect()->intended('/admin');
}
return redirect()->intended('/home');
}

Laravel same route, different controller

I would like to have general home page
and a different homepage for logged-in users
I search a lot on google but I can't find what to put in my if statement
I tried something like this:
Route::get('/', array('as'=>'home', function(){
if (!Auth::check()) {
Route::get('/', array('uses'=>'homecontroller#index'));
}
else{
Route::get('/', array('uses'=>'usercontroller#home'));
}
}));
I also try with something like:
return Controller::call('homecontroller#index');
but it seems it's not for laravel 4
I tried a lot of other things so I think it's more a misconception problem
If you have any clue
thanks for your help
ok after discussions on this platform and other forums, I come back with a compact solution
Route::get('/', array('as'=>'home', 'uses'=> (Auth::check()) ? "usercontroller#home" : "homecontroller#index" ));
The most simple solution I can think of is:
<?php
$uses = 'HomeController#index';
if( ! Auth::check())
{
$uses = 'HomeController#home';
}
Route::get('/', array(
'as'=>'home'
,'uses'=> $uses
));
Or you can just route the url / to method index() and do the Auth::check() in there.
// routes.php
Route::get('/', 'homecontroller#index');
// homecontroller.php
class homecontroller extends BaseController
{
public function index()
{
if (!Auth:: check()) {
return $this->indexForGuestUser();
} else {
return $this->indexForLoggedUser();
}
}
private function indexForLoggedUser()
{
// do whatever you want
}
private function indexForGuestUser()
{
// do whatever you want
}
}
You should try something like:
Route::get('/', array('as'=>'home', function(){
if (!Auth::check()) {
Redirect::to('home/index'));
}
else{
Redirect::to('user/index'));
}
}));
So you are basically redirecting the user based on the Auth check instead of defining an additional route.
Or use route filters
Route::filter('authenticate', function()
{
if (!Auth::check())
{
return Redirect::to('home/index');
}
});
Route::get('home', array('before' => 'authenticate', function()
{
Redirect::to('user/index');
}));
http://laravel.com/docs/routing#route-filters

Categories