how to setup routes, controller, and views the proper way in Laravel? - php

I just started playing around with laravel. First of all, this is the best framework i've came across. Now here is my issue. I am trying to point from routes -> controller -> view
//This is my Controller file
public function index()
{
return View::make('pages', array('name' => 'Taylor'));
}
// This is my Routes File
Route::get('/', 'pagesController#index');
View file => pages.blade.php
This is the error i'm getting.
FatalErrorException in pagesController.php line 19:
Class 'App\Http\Controllers\View' not found

From the error it seems that the problem is that the View class is not found in the current namespace.
Try this way:
//use the View class from the global namespace
return \View::make('pages', array('name' => 'Taylor'));
or import the View class at the beginning of the controller's script:
use View;

Related

Call to undefined method Illuminate\Support\Facades\App::index()

I'm new to learning Laravel but I'm having trouble routing to controller, I have a controller named "App" and I have a function named index in it, it says it can't find it in "App" controller even though I set it in the route
Error
Error
Call to undefined method Illuminate\Support\Facades\App::index()
http://localhost:8000/anasayfa
App.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class App extends Controller
{
public function index(){
return "anasayfa";
}
}
web.php
Route::get('/anasayfa', 'App#index');
What is the reason for this error?
A class with the name App already exists in Laravel, defined in namespace Illuminate\Support\Facades\App
if you want to use your class make sure to add
use App\Http\Controllers\App
in your web.php
It is recommended to use a different name. You should follow the conventions and name it AppController.
I'm solved.
I deleted the controller named "App" and created a controller named "AppController". But this caused a new error, Laravel could not find class "AppController". For this I updated web.php as follows;
use App\Http\Controllers\AppController;
Route::get('/anasayfa', [AppController::class, 'index']);

InvalidArgumentException - No hint path defined

I am trying to add controller view in my existing project that consider model view controller structure in php with laravel.
class CashFlowdataController extends Controller {
public function index() {
return view('CashFlowdata::create');
}
}
When I implement this, it shows me error for,
InvalidArgumentException
No hint path defined for [CashFlowdata].
I have added file in route.php and web.php as added other controller data. Only for this one it shows message like this.
you code is wrong you should to something like this
class CashFlowdataController extends Controller {
public function index() {
return view('CashFlowdata.create');
}
}
here CashFlowdata.create
its means in laravel
folder structure should be
view>CashFlowdata>create.blade.php
laravel view() function is a helper to load view file
ref link https://laravel.com/docs/8.x/helpers#method-view
I had the same issue in nwidart/laravel-modules due to module.json file was miss place.
I move the file to the root of module now working fine.

Is there a way to use auth() or other function outside app\Http\Controllers folder?

I want to use access keys for responsivefilemanager.
Since access keys can be seen by the users(example.com/filemanager/dialog.php?akey=usersaccesskeys), I want the access keys to be temporary(single use only). But I think I need "auth()" and other functions like "User::find($user_id)" to do this.
Anyways there is an error when I use auth() or controller functions in config file of the filemanager, "Call to undefined function auth() in /path/to/config/config.php".
I am stuck and I don't want to deploy my website like this.
Is there any other ways to make responsivefilemanager secure?
I have also tried using controller php file in app\Http\Controllers\RfmController.php
Other error shows "Uncaught Error: Class 'App\Http\Controllers\Controller'"
This is the function RfmController extends to.
I am running LAMP server.
PHP 7.3.7
Laravel 5.8
In my config.php
'access_keys' => array(auth()->user()->name),
by using controller.php
In my config.php
namespace App\Http\Controllers;
require('/path/to/app/Http/Controllers/RfmController.php');
$rfm = new RfmController;
.
.
.
'access_keys' => array(RFMClass::rfmakey()),
In RfmController.php
namespace App\Http\Controllers;
class RfmController extends Controller
{
public function rfmakey()
{
return auth()->user()->id;
}
{
I expected that rfmakey() would return the username
You need to use middleware. From the official laravel documentation:
If you are using controllers, you may call the middleware method from the controller's constructor instead of attaching it in the route definition directly:
public function __construct()
{
$this->middleware('auth');
}
As an aside, I would stay away from putting dynamic values or code in my config.php file, like
auth()->user()->name
This is not a good practice.
Hope this solution helps you!

How can I correctly setup models in Laravel 5 and fix my error?

I am currently learning Laravel 5. I have connected my database. Setup routes, created a controller, created a view and attempted a model, which is where I need help.
I used php artisan to create my model, which is in the /app directory.
When I try to visit /myer on the browser. I am getting the following error:
FatalErrorException in MyersController.php line 20:
Class 'App\Http\Controllers\Myer' not found
I have put the edited files on http://www.filedropper.com/help
I have no idea where I have gone wrong, I have messed around using "use" and ultimately all I get is that the Class can't be found. This is beginning to destroy my soul. If someone can help me, I would be forever grateful!!
Files
From MyersController.php
public function index()
{
$myers = Myer::all();
return view('myers.index')->with('myers'.$myers);
}
From routes.php
Route::get('/myer/', 'MyersController#index');
Route::resource('myer','MyersController');
From Myer.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Myer extends Model
{
//
}
From index.blade.php
<h2>Myers</h2>
<ul>
#foreach ($myers as $list)
<li>{{{ $list->name }}}</li>
#endforeach
</ul>
As you can see in the error, it tries to find the model in the same namespace as your controller: FatalErrorException in MyersController.php line 20: Class 'App\Http\Controllers\Myer' not found. In the Model, you can see it's in namespace App.
So either put
use App\Myer;
in the top of your controller under the namespace, or reference the full path where you need it:
public function index()
{
$myers = App\Myer::all();
return view('myers.index')->with('myers'.$myers);
}
But if you will use it more often in this controller, then it's more efficient to put it in the use.
P.S. Please don't let it destroy your soul.

url in routes.php not rendering in laravel 4

I am new to laravel. I am just trying to do some basic stuff using routes and controllers.
I have this controller named testcontroller which looks as below
<?php
class testcontroller extends BaseController{
public function show(){
return 'test';
}
}
I have also defined a route in routes.php as below
Route::get('test', 'testcontroller#show');
when i try to access it using localhost/laravel/public/test it would give the 404 NOT FOUND error, however i have tried rewriting the route in main directory as
Route::get('/', 'testcontroller#show');
and it works all fine, i have even tried and successfully rendered views using this. Can anyone suggest where am i going wrong with the url. Any help will be appreciated.
Thanks :)
Test controller
# app/controllers/TestController.php
class TestController extends BaseController {
public function show()
{
return 'test';
}
}
Routes File
# app/routes.php
Route::get('test', array(
'uses' => 'TestController#show'
));
Route::get('/', array(
'uses' => 'TestController#show'
));
Edit: since that is not working, it is probably to do with your server configuration. Instead of learning how to do that, I'd recommend the use of a VM over WAMP. Also Laravel provides an easy way to use one with Laravel Homestead.
Check out this instructional video on how to do that.

Categories