im trying to redirect a controller to a controller but im getting the MethodNotAllowedHttpException in RouteCollection.php line 218: error and i cant seem to figure whats wrong
commentcontroller:
<?php
namespace App\Http\Controllers;
use Session;
use Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class commentcontroller extends Controller
{
public function create()
{
$linked_to_post = Request::input('linked_to_post');
$creator_id = Request::input('creator_id');
$comment = Request::input('comment');
DB::table('comments')->insert(['linked_to_post'=>$linked_to_post,'creator_id'=>$creator_id,'content'=>$comment]);
return redirect()->action('postcontroller#post', ['redirectid' => $linked_to_post]);
}
postcontroller:
<?php
namespace App\Http\Controllers;
use App\Users;
use Session;
use App\posts;
use Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class postcontroller extends Controller
{
public function post(){
if (isset($redirectid)) {
$currentid = $redirectid;
}else{
$currentid = request::input('hiddenpostid');
}
$users = users::getusers();
$posts = posts::getposts();
foreach ($posts as $post) {
if ($currentid == $post->post_id) {
$currentpost = $post;
}
}
return view('post',['posts'=>$currentpost]);
}
routes:
Route::get('/', function () {
return view('welcome');
});
Route::get('new','productcontroller#product');
Route::get('admin','admincontroller#authenticate');
Route::get('blog','postcontroller#index');
Route::post('createpost','postcontroller#create');
Route::post('registeruser','usercontroller#create');
Route::post('loginuser','usercontroller#login');
Route::post('logoutuser','usercontroller#logout');
Route::post('post','postcontroller#post');
Route::post('submitcomment','commentcontroller#create');
You are attempting to redirect to a POST route. Redirects make a GET request.
Thus you get a MethodNotAllowedHttpException as you do not have a GET method route set up for the /post uri.
Related
I am creating user authentication using a custom table. In my login controller authentication is working fine and redirected to dashboard. But when I am going to create another url using a new controller, user auth data not showing for that controller.
I want to get user data through auth facade in constructor. How will that possible?
Here is my code:
web.php:
<!---Routes for login and dashboard-->
Route::get('/login','CustomLogin#index');
Route::post('/login','CustomLogin#checklogin');
Route::get('/','CustomLogin#SuccessLogin');
Route::get('/logout','CustomLogin#logout');
<!---Routes for other controller where user auth not working-->
Route::get('/add-creditor', 'AddCreditor#index');
CustomLogin.php (controller):
<?php
namespace App\Http\Controllers;
use App\library\My_functions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\User;
use Illuminate\Support\Facades\Auth;
use Redirect;
use View;
use Session;
use Cookie;
class CustomLogin extends Controller
{
public function __construct()
{
$this->_myFun = new My_functions;
}
public function index()
{
if(!Auth::check()) {
return view('CustomLogin.CustomLogin');
}
else{
Redirect::to(SITE_URL)->send();
}
}
public function username()
{
return 'user_name';
}
function checklogin(Request $request)
{
$this->validate($request, [
'input-username' => 'required',
'input-password' => 'required'
]);
$user_data = array(
'user_name' => $request->get('input-username'),
'password' => $request->get('input-password')
);
if(Auth::attempt($user_data)){
return redirect('/');
}
else
{
return back()->with('error','Wrong Login Details');
}
}
function SuccessLogin(){
if (!$this->_myFun->DoLogIn()) {
Redirect::to(SITE_URL.'login')->send();
}
else {
$data=array();
return View::make('include.dashboard',$data);
}
}
function logout(Request $request){
Auth::logout();
return redirect('/login');
}
}
Function DoLogIn() (app/library)
<?php namespace App\library {
use Illuminate\Routing\Controller as BaseController;
use App\library\CreateCrmGuid; // Get custom function
use App\library\FunctionForContact; // Get custom function
use Illuminate\Http\Request;
use Session;
use DB;
use Hash;
use Auth;
use App\User;
class My_functions{
public function DoLogIn()
{
//dd(Auth::user()); /*returns data if run from Login controller but null from Add Creditor controller*/
if(Auth::check())
{
$user_id = Auth::user()->id;
define('authenticated_user_id' ,$user_id);
return true;
}
else
{
return false;
}
}
}
AddCreditor Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Cookie;
use Config;
use App\library\my_functions; // Get custom function
use Redirect;
use DB;
use Session;
class AddCreditor extends Controller
{
protected $_myFun;
function __construct(){
dd(Auth::user()); // this returns null
$this->_myFun = new My_functions;
if (!$this->_myFun->DoLogIn()) {
Redirect::to(SITE_URL.'login')->send();
}
}
}
Add auth Middleware in your routes
Route::middleware(['auth'])->get('/add-creditor', 'AddCreditor#index');
Still, after this, you might not get user data through Auth facade in the controller constructor, But in your Route method i.e. AddCreditor#index you will get the user data either through the following methods
Auth::user();
or
request()->user();
I'm trying to display data in my views in Laravel 5.6.9, but I keep getting this error.
Error
Code snippet
TodosController
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with('todos', '$todos');
}
}
Browser gave this error
<div class="title m-b-md">
<?php $__currentLoopData = $todos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $todo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php echo e($todo->todo); ?>
<br>
In your controller, you must remove the single quotes around the $todos variable:
return view('todos')->with('todos', $todos);
You should change your controller code like:
namespace App\Http\Controllers;
use App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller
{
public function index() {
$todos = Todo::get();
return view('todos',compact('todos'));
}
}
A wiser approach to the situation would be to use compact. Compact is a PHP function that creates an array containing variables and their values.
When returning a view, we can easily use compact to pass some data.
One can use compact like this:
$data = Data::all();
return view('viewname')->with(compact('data'));
So in your script:
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with(compact('todos'));
}
}
If you wish to do it the way you tried it in the first place, you should do it like this:
<?php
namespace App\Http\Controllers;
use\App\Todo;
use Illuminate\Http\Request;
class TodosController extends Controller {
public function index()
{
$todos = Todo::all();
return view('todos')->with('todos', $todos);
}
}
Mind that there are no apostrophes around the variable $todos.
When I try to call API then I got this error.
Here is My Controller
<?php
namespace App\Http\Controllers;
use App\Video;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class VideoController extends Controller
{
public function index(){
//return 'index function of video controller';
$Video = DB::table('videos')->get();
return responce()->json($Video);
}
}
?>
here is my web.php
$app->get('/', 'VideoController#index');
You should return $Video or any other variable or array, not a function response()
EDIT
try to just return json_encode($Video);
I want to display the content saved on the database on my view page. I've applied relationship between user and account.
I have another relationship between user and post which is working perfectly.
The error I am getting is:
ErrorException in 0401a2c6f4dbf412d9f16ba1e4ced9e54c8bb622.php line 132:
Undefined variable: accounts (View: E:\wamp\www\gal\resources\views\myplace.blade.php)
my view code:
<form action="{{route('account')}}" id="acc" method="post">
<div class="col-lg-3"><label>Estado</label>
#foreach($accounts as $account)
<textarea style="display:none;" name="textfield4" id="textfield4">{{$account->estado}}</textarea></div>
<div class="col-lg-2 es" id="estado"></div>
#endforeach
my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\Account;
use Illuminate\Support\Facades\Auth;
class AccountController extends Controller
{
public function account(Request $request)
{
$account = new Account();
$account->estado = $request['textfield4'];
$request->user()->accounts()->save($account);
return redirect()->route('myplace');
}
public function getaccount()
{
User::find(Auth::id())->accounts;
$accounts = Account::orderBy('created_at','desc')->get();
return view('myplace',['accounts'=>$accounts]);
}
}
Given this code...
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\Account;
use Illuminate\Support\Facades\Auth;
class AccountController extends Controller
{
public function account(Request $request)
{
$account = new Account();
$account->estado = $request['textfield4'];
$request->user()->accounts()->save($account);
return redirect()->route('myplace');
}
public function getaccount()
{
User::find(Auth::id())->accounts;
$accounts = Account::orderBy('created_at','desc')->get();
return view('myplace',['accounts'=>$accounts]);
}
}
$account->estado = $request['textfield4']; should be $account->estado = $request->input('textfield4', 'some_default_value');
User::find(Auth::id())->accounts; does literally nothing as you don't save the result, and $accounts = Account::orderBy('created_at','desc')->get(); is sorting all accounts.
As for how none of your Accounts are being shown with orderBy is probably due to not having any query about what to select or conditions for it to be true. Default values may help, but if you have something funky it may be broken. There isn't much we can do without more code.
Another guess I have about this is return view('myplace',['accounts'=>$accounts]); should be return view()->make('myplace',['accounts'=>$accounts]);
EDIT:
His problem was he was navigating to /myplace which doesn't route to getaccount() it routes to getmyplace(), which didn't have the $accounts variable passed.
Instead of doing:
return view('myplace',['accounts'=>$accounts]);
You can use compact in your Controller:
return view('myplace')->with(compact('accounts'));
route in routes.php
Route::get('korisnici', array('uses'=>'MojPrviKontroler#prvaAkcija'));
//
my controler in Controllers
<?php
use App\User;
use App\Http\Controllers\Controller;
class MojPrviKontrolerController extends Controller
{
public $restful = true;
public function get_prvaAkcija()
{
return View::make('prviViewovi.PrviView.php');
}
}
Can somebody tell my why my controller isn't found?
Seems like a wrong namespace problem.
Change to this:
<?php
namespace App\Http\Controllers;
use App\User;