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;
Related
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(Auth::Attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Change the Auth namespace to:
use Illuminate\Support\Facades\Auth;
You may use the auth helper instead and then no worries about any class name
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(auth()->attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Try this :
\Auth::attempt([...]);
I think this is helpfull..
Use the Auth namespace to:
use Illuminate\Support\Facades\Auth;
When i execute my code that returns:
Class 'app\Nota' not found
I try use app\Notas::all(); instead app\Nota::all();, in the controller, but didn't work. I try too use app\Notas; instead app\Nota; but didn't worked for me.
My model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Nota extends Model
{
//
}
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Nota;
class productoController extends Controller
{
public function show($id){
$notas = app\Nota::all();
print_r($notas);
die;
}
}
What can be the problem?
Replace your controller code like -
<?php
namespace App\Http\Controllers;
use App\Nota;
use Illuminate\Http\Request;
class productoController extends Controller
{
public function show($id){
$notas = Nota::all();
dd($notas);
}
}
Try using the following:
$notas = Nota::all();
and replace use app\Nota; by use App\Nota;
Hello i am trying to use a trait from controller in my register controller but it can't seem to find it
the error message:
Trait 'MailVerification' not found
The class in which i want to use the trait
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
use MailVerification;
Here i call the function
protected function create(array $data)
{
$mail = $data['email'];
$this->sendVerification($mail);
Here is the trait in the class i am trying to import it from
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\TestMail;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Session;
trait MailVerification
{
public function sendVerification($mail)
{
$verification_code = str_random(30);
Mail::send('mail.verify', ['verification_code' => $verification_code, 'mail' => $mail], function ($message) use ($mail)
{
$message->from('test#laravel.com');
$message->to($mail);
});
Session::flash('message', "Please check you're email to verify your account");
return redirect('/');
}
}
class MailController extends Controller
{
I have the trait outside of my class i don't know if this is correct but it was giving me an error while it was inside the class.
The namespace of your controller RegisterController and your trait MailVerification is different...
So, you'll have to add this line to your RegisterController
use App\Http\Controllers\MailVerification;
Also, I suggest you to put all your traits inside App\Traits folder instead of your controller. Try following a simpler way if possible :)
Edit --
This is how you register controller should look like
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Controllers\MailVerification;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers, MailVerification;
//Your code here....
}
I have following function and want to call it from view. Basically i want to put all common functions in one file. I am not sure where to create that file and how to call it inside controller and view.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
So far i have created CommonController.php in app/Http/Controllers and put above function in it.
Then in other controller i have tried to call it following way:
use App\Http\Controllers\Common;
class SongsController extends Controller {
public function index($id)
{
echo Common::BytesToMB('7012187');
}
}
But i am getting error:
Class 'App\Http\Controllers\Common' not found
Ok, new try. You missed to use the complete class name and add the static keyword:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public static function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends Controller {
public function index($id)
{
echo CommonController::BytesToMB('7012187');
}
}
Another and more OOP solution is to use the function from the parent class:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
protected function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends CommonController {
public function index($id)
{
echo $this->bytesToMB('7012187');
}
}
So, in L5 I created folder like app/Models/Blog where is file Posts.php which looks like:
<?php namespace App\Models\Blog;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model {
protected $table = 'posts';
}
After it I executed composer dump and then in my controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Models\Blog\Posts as Posts;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
It throws me an error:
FatalErrorException in BlogController.php line 14: Class 'Models\Blog\Posts' not found
Try changing this:
use Models\Blog\Posts as Posts;
To this:
use App\Models\Blog\Posts;
In Laravel 5.2 it's just:
use App\Blog;
or
use App\Blog\Posts;
Change the following
class Posts extends Model {
to
class Posts extends \Eloquent {
You need to check two points :
the namespace have to be in first
the using must be use App\Models\Blog in your case
Like this :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Blog;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
(tested with Laravel 5.4)