View not display in laravel - php

I have install laravel in my local server. I have route with following code
Route::get ( '/', function () {
return view ( 'welcome' );
});
Route::get('/home', 'HomeController#index');
And controller file with following code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
echo 'test';
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
echo 'test1';exit;
return view('welcome');
}
}
But i am not able to access my view file name welcome.blade.php in resources/views directory.
i have try following urls in web browser but i got blank page every time.
URL 1:
http://localhost/StripeIntegration_laravel-master/public/home
URL 2:
http://localhost/StripeIntegration_laravel-master/public/index.php/home
URL 3:
http://localhost/StripeIntegration_laravel-master/public/
Please help me to solve out this.

I check your code if your current code runs then Your output display will appear like this:
testtest1
If you remove exit; from your index function then Your output display will appear like this:
testtest1 //And along with the code of your welcome file will also be printed here
HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function __construct()
{
echo 'test';
}
public function index()
{
echo 'test1';
return view('welcome');
}
}
web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController#index')->name('home');

Please remove exit; from your index function
public function index()
{
echo 'test1';
return view('welcome');
}

You shouldn't have public in your URL.
Your webserver should be pointing at the public directory as the web root and the URL you need should be http://localhost/StripeIntegration_laravel-master/home

first of all, remove the echo 'test'; in the construct function, then I want to say to u that this code doesn't make sense at all: exit; return view('welcome');
in your controller. if you go to localhost/yourprojectname/public it should show the welcome view since the root in your route says it returns the welcome view :
Route::get ( '/', function () {return view ( 'welcome' );});
if u got any questions, feel free to ask

Related

Laravel- Redirecting users with a dynamic parameter through controller

I have set up the web routing for my url to show as per
localhost/testapp/public/{user}/hello
with this current setting
Route::prefix('{user}')->middleware('user')->group(function () {
Route::prefix('welcome')->group(function () {
Route::view('/', 'layout.index')->name('index');
Route::view('thank-you', 'layout.thank-you')->name('thankyou');
});
Route::get('/', 'HomeController#redirect');
});
Route::view('/', 'layout.index')->name('index'); this line works fine the way i wanted it to work, however I wasn't able to get the HomeController#redirect to work the way I want it to.
The idea is for the user to be redirected when localhost/testapp/public/{user} is being entered, it would redirect user to the page localhost/testapp/public/{user}/hello
This is how the HomeController looks
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function redirect(Request $request)
{
return redirect(['user' => $request, config('settings.default_user')].'/welcome');
}
}
whereby if the dynamic parameter is somehow not received, I would stick to the default user "admin" that is being stored in my settings.php within the config folder.
Use an optional parameter in the routes file, otherwise it will show a 404 if no {user} is provided
Route::prefix('/{user?}')
Then with this in config/settings.php
<?php
return ['default_user' => 456];
And this in the HomeController
public function redirect(Request $request)
{
if (empty($request->user)) {
return redirect(config('settings.default_user') . '/welcome');
} else {
return redirect($request->user . '/welcome');
}
}
Now when I try in my browser
/123 redirects me to /123/welcome.
/ redirects me to 456/welcome.

Can not access the user register or login page while login as admin

I am using laravel auth and trying to access the user login and register page while I am login as admin. But i can not access, I think there must be some changes in guards and providers. please help
I have only one table where I have defined the admin separately. fig is given.
Above the pictures from user table where for admin I assign 1 and for all others users assign zero. I can login on both and redirected on seperate views for user and admin. but on same time I can not access both user and admin.
HomeController.php code is given.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use DB;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
public function allusers()
{
$key['key'] = DB::table('users')->get();
return view('allusers',$key);
}
}
Code of routes is given here.
Auth::routes();
Route::group(['middleware' => ['web','auth']],function(){
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', function(){
if (Auth::user()->admin==0) {
return view('home');
}
else {
$users['users'] = \App\User::all();
return view('adminhome', $users);
}
});
});
Please help what kind of changes I can do in guards and providers so I can access both on same time.
In your app/Http/Controllers/Auth/RegisterController.php you have a __construct() that checks if the user is guest then continue and you should change it
public function __construct()
{
$this->middleware('guest');
}
and you should change it like bellow to check that user is login
public function __construct()
{
$this->middleware('auth');
}
Note: By this change just logged in user will access to the registration page you should customize the condition as you want.
have a view on this link for log as admin
https://stackoverflow.com/a/42473137/9246297

Routes Redirecting to the login page Laravel 5.2

I am using Entrust for the authentication of my Laravel application. I don't know why but I can't access to my public routes as a guest.
This route is working for guests:
Route::get('/course-calendar', function () {
$events = \App\Models\Event::all();
return view('public.calendar.index' , compact('events'));
});
But when I use a route like this:
Route::resource('courses' , 'CourseController');
redirects me to the login page. These two routes are at the top of the route.php
Here is my controller:
<?php namespace App\Http\Controllers;
use App\Models\Event; //models are at App\Models
class CourseController extends Controller
{
//Show lists of the events in the calendar
public function dekha()
{
$events = Event::all();
return view('public.calendar.index' , compact('events'));
}
//show single page
public function show($id)
{
$event = Event::find($id);
if (is_null($event))
{
return Redirect::route('courses');
}
return View::make('public.events.single', compact('event'));
}
}
It seems to be weird . Can you please point out what I am missing ?
I was doing a silly mistake in my Controller.php .
I had this:
public function __construct(){
$this->middleware('auth');
}
I had to remove the code block from controller.php

Laravel using Controller#show return view and api

I am making a web application that will also be turned into a native app so I want to show json from one path and return a view for another path. Here is what I was thinking but not sure how to write the if statements in Laravel:
Routes.php
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/notes', 'NotesController#index');
Route::get('/notes/{note}', 'NotesController#show');
Route::get('api/notes/{note}', 'NotesController#show');
});
NotesController.php
<?php
namespace App\Http\Controllers;
use App\Note;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class NotesController extends Controller
{
public function index() {
$notes = Note::all();
return view('notes.index', compact('notes'));
}
public function show(Note $note) {
//if its from the general path show view
return view('notes.show', compact('note'));
//if its from the api path show json
return $note;
}
}

Object not found error - When creating new pages in Laravel

I am new in Laravel. I am trying to create a new page named as "contact". But i am getting a Object not found error when i am trying to access the contact page
URL: project-name/contact
please help me
---routes file
<?php
Route::get('/','WelcomeController#index');
Route::get('contact','WelcomeController#contact');
Route::group(['middleware' => ['web']], function () {
//
});
--- Welcome controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class WelcomeController extends Controller
{
public function index(){
return view('welcome');
}
public function contact(){
return 'Contact page goes here...';
}
}
Set your home directory to the public to make things work.
Exchange the route. like this:
Route::get('contact','WelcomeController#contact');
Route::get('/','WelcomeController#index');

Categories