I'm new to Laravel so I'm not familiar with errors in the framework .I'm trying to get the user to make a post but I'm getting the above error .Could you please tell where I'm going wrong ?Thank you
This is my HomeController class:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
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(Request $r)
{
if(Input::has('status-text'))
{
$text = e(Input::get('status-text'));
$userStatus = new Status();
$userStatus->status_text = $text;
$userStatus->save();
Flash::success('Your status has been posted');
return redirect(route('home'));
}
return view('home');
}
}
And this is my web.php class :
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::any('/home', ['as'=> 'home','uses' =>'HomeController#index']);
Don't use Input::get(), use $r->get() as you're injecting the request as a dependency to the index method already, and Input:: is merely a alias to access the underlaying Request.
Related
I'm new with laravel fortify and I'm trying to create more than one account for my project, but whenever I register the first account and try to access again the register page, it keeps redirecting me to home page. Please let me know if I need to post any more code here.
Here's my FortifyServiceprovider:
<?php
namespace App\Providers;
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Fortify;
use App\Responses\RegisterResponse;
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
class FortifyServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(
RegisterResponseContract::class,
RegisterResponse::class
);
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::loginView(function(){
return view('index');
});
Fortify::registerView(function(){
return view('register');
});
RateLimiter::for('login', function (Request $request) {
return Limit::perMinute(5)->by($request->email.$request->ip());
});
RateLimiter::for('two-factor', function (Request $request) {
return Limit::perMinute(5)->by($request->session()->get('login.id'));
});
}
}
for redirect after register
app -> Providers -> RouteServiceProvider
public const HOME = '/home';
to
public const HOME = '/';
I have this situation where I have a route /admin which requires Auth middleware to be activated. I have the middleware requirement indicated in the web.php route. Also, I do have the default auth setup of laravel.
The kernel.php does have the middleware indicated too.
But, weirdly enough /admin brings me to a white page with nothing. When logged in, the problem isn't there. It had been working and all of a sudden it was not working anymore.
The auth middleware is as it is:
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* #param \Illuminate\Http\Request $request
* #return string
*/
protected function redirectTo($request)
{
return route('login');
}
}
The controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\NewsletterSubscribers;
use App\User;
use File;
class adminController extends Controller
{
//
public function __construct()
{
$this->middleware('auth');
$this->middleware('admin');
}
public function index(){
return view('admin.home');
}
public function changebg(){
return view('admin.changebg');
}
public function changebgimage(Request $request){
$this->validate($request,
[
'image'=>'required|image|mimes:jpg,JPG,jpeg,JPEG|max:4096|dimensions:max_width:1600,max_height:1100',
]
);
$path="images/";
$imagepath="images/bg.jpg";
if( File::exists($imagepath))
{
unlink($imagepath);
}
if ( ! File::exists($path) )
{
File::makeDirectory($path,0777,true);
}
$getimageName = "bg.jpg";
$request->image->move(public_path($path), $getimageName);
return view('admin.home');
}
public function newslettersubscriberslist(){
$newslettersubscribers= NewsletterSubscribers::all();
$count=0;
return view('admin.subscriberslist',compact('newslettersubscribers','count'));
}
public function registerAdmin(){
return view('auth.adminregister');
}
public function viewAdmins(){
$admins= User::select('users.*')->where('role','=','admin')->get();
//print_r($admins);
$count=0;
return view('admin.adminlist',compact('admins','count'));
}
public function viewUsers(){
$users= User::select('users.*')->where('role','=','user')->get();
//print_r($admins);
$count=0;
return view('admin.userlist',compact('users','count'));
}
}
The admin middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
class Admin
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::check() && Auth::user()->role == 'admin') {
return $next($request);
}
else {
return redirect('/login');
}
}
}
The the route I'm using:
Route::get('/admin', 'AdminController#index')->name('adminhome')->middleware('auth');
I dont find anything weird here but weirdly enough the problem exists. Can you guys trace somethin unusual here or somewhere it can be??
First of all, make sure you have error reporting turned on. Also take a look at laravel log. Looking at your code the problem might be case of AdminController. In routes you have 'AdminController#index' but the class you showed has name adminController and it should be AdminController. I also don't know what is the name of file but it should be again AdminController.php
You can use the middleware like this
Route::group(['middleware'=>'auth'], function()
{
Route::get('/admin', 'AdminController#index')->name('adminhome');
}
The problem that I'm having in my web app is that when a user types up a status and presses "add status" instead of it saving the status I get the following error : NotFoundHttpException in RouteCollection.php (line 179).
I have done some research online and looked at similar problems and tried changing it around but I still got the same problem back .
This is my Controller Class
<?php
namespace App\Http\Controllers;
use Request;
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(Request $r)
{
if(Request::has('status-text'))
{
$text = e(Request::get('status-text'));
$userStatus = new Status();
$userStatus->status_text = $text;
$userStatus->user_id = Auth::user()->id;
$userStatus->save();
Flash::success('Your status has been posted');
return redirect(route('home'));
}
return view('home');
}
}
This is my web.php class
<?php
return view('welcome');
});
Auth::routes();
Route::any('/home', ['as'=> 'home','uses'=>'HomeController#index']);
This is an image of the error that I'm getting back :
First thing is you can just have return view() in the routes/web.php file. All you should need to do is make your web.php like this:
Auth::routes();
Route::get('/home','HomeController#index');
that should fix your problem.
Actually I'm very new of the php and Laravel5.3,and I have this problem when i want to set request of the form(Doing a simple blog page)
ReflectionException in Route.php line 339: Class App\Http\Controllers\Requests\ArticleRequest does not exist
And this is my controller code(Filename:ArticlesControllers.php):
<?php
namespace App\Http\Controllers;
//namespace App\Http\Controllers;
use App\Http\Requests\ArticleRequest;
use App\Article;
use Carbon\Carbon;
use Illuminate\Http\Request;
class ArticlesControllers extends Controller
{
//
public function index(){
$articles = Article::latest()->get();
//return 'articles';
return view('articles.index')->with('articles',$articles);
}
public function show($id){
$article = Article::find($id);
// if(is_null($article)){
// abort(404);
// }
//dd($artilce);
return view('articles.show',compact('article'));
}
public function create(){
return view('articles.create');
}
public function store(Requests\ArticleRequest $request){
//dd($request->all());
//接受post过来的数据
//存入数据库
//重定向
$input=$request->all();
//$input['published_at']=Carbon::now();
Article::create($input);
return redirect('/articles');
}
}
And the request File code:(Filename:ArticleRequest.php in the path Requests)
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ArticleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'title'=>'required|min:3',
'content'=>'required',
'published'=>'require'
];
}
}
My Route/Web.php is:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//Route::get('/','SiteController#index');
Route::get('/', function () {
return view('welcome');
});
Route::get('/articles','ArticlesControllers#index');
Route::get('/articles/create','ArticlesControllers#create');
Route::get('/articles/{id}','ArticlesControllers#show');
Route::post('/articles/store','ArticlesControllers#store');
How can i get rid of this nasty problem,I've been searching on StackOverFlow but nearly all the answers may not solve it....
Change it to this to make it work:
public function store(ArticleRequest $request)
you already imported the class at the top
use App\Http\Requests\ArticleRequest
no need to use a full class name as an argument
just use
public function store(ArticleRequest $request)
or
public function store(\App\Http\Requests\ArticleRequest $request)
I started using Laravel today, but I have some problems. Controllers don't run.
This is my controller:
<?php
class HomeController extends Controller {
/*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Show the application welcome screen to the user.
*
* #return Response
*/
public function index()
{
return view('welcome');
}
public function contact(){
return view(pages.contact);
}
?>
and this is my route:
<?php
Route::get('/', function () {
return "hello";
});
Route::get('contact','HomeController#contact');
?>
You need to add the namespace to the beginning of the controller:
<?php
namespace App\Http\Controllers;
You can also run this command when creating a controller
php artisan make:controller HomeController
In, addition as the other answer mentioned, the view name needs to be inside quotes.
Hope this helps.
This should be like this
public function contact(){
return view('pages.contact'); // View name must be inside ' '
}
also you dont need the closing tag for php ?>