simple Laravel View::make() not working - php

So I'm learning some basic Laravel stuff as I am new to PHP.
I am following a basic tutorial that is having me print stuff to a file named home.blade.php.
The way I am calling it now is as follows.
Here is my routes.php
Route::get('/', array(
'as' => 'home',
'uses' => 'HomeController#home'
));
Here is my HomeController.php
class HomeController extends Controller {
public function home() {
return View::make('home');
}
}
Here is home.blade.php
{{'Hello.'}}
Before you ask, yes my home.blade.php is inside of my Views folder.
The error print out is as follows
FatalErrorException in HomeController.php line 6:
Class 'App\Http\Controllers\View' not found
in HomeController.php line 6
at HandleExceptions->fatalExceptionFromError(array('type' => '1', 'message' => 'Class 'App\Http\Controllers\View' not found', 'file' => '/Users/ryandushane/Google Drive/Web_WorkSpace/theNeonSurf/app/Http/Controllers/HomeController.php', 'line' => '6')) in compiled.php line 1738
at HandleExceptions->handleShutdown()
Here's the odd part. If I change my routes.php to simply contain
Route::get('/', function()
{
return View::make('home');
});
it functions fine.
Does anyone have an idea of what I could do?

New syntax for Laravel 5
public function home() {
return view('home');
}
For more information you can read it from here http://laravel.com/docs/5.0/views

Try this in your top of the class(controller)
use View;

I bet your controller class has a namespace, yes? Try \View::make('home'); Or you can import it in the top of the file:
<?php
namespace App\Http\Controllers;
use View;
class HomeController extends Controller {
public function home() {
return View::make('home');
}
}

Related

Laravel : Overriding route for specific route in voyager

I am trying to override a route for creating a row. (posting, not viewing)
http://lsapp.dev/admin/cpu-speed/create
In web.php
I modified
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
Route::post('/cpu-speed',['uses' => 'Admin\Mobiles\CPUSpeedController#store', 'as' => 'store']);
});
Also I created Controller
namespace App\Http\Controllers\Admin\Mobiles;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class CPUSpeedController extends Controller
{
public function store(){
return 'hello';
}
public function create(){
return 'create';
}
}
But it throws the following error:
ErrorException (E_ERROR) Route [voyager.cpu-speed.store] not defined.
(View:
/var/www/html/lsapp/vendor/tcg/voyager/resources/views/bread/edit-add.blade.php)
It appears you are only naming it store here:
Route::post('/cpu-speed',['uses' => 'Admin\Mobiles\CPUSpeedController#store', 'as' => 'store']);
It should probably be:
Route::post('/cpu-speed',['uses' => 'Admin\Mobiles\CPUSpeedController#store', 'as' => 'voyager.cpu-speed.store']);
I'm not entirely sure this will work, since it may be interpreted and descend into the Voyager package, rather than just reading your web.php file, but I believe it will do what you like.

Laravel 5 named route to controller action

I have a REST Controller which I extended with a new action verify(). Now I want to call this action via a named route, but when I open www.foo.bar/verify I get an error:
BadMethodCallException in Controller.php line 273:
Method [verify] does not exist.
When I call the action create instead in the routes.php it works surprisingly.. This is a kind of strange and I have now glue where my error is...
How can I can I call my verify() action with a name route?
app/Http/routes.php
Route::get('/', 'WelcomeController#index');
Route::resource( 'activation', 'ActivationController' );
Route::get( 'verify', [ 'as' => 'verify', 'uses' => 'ActivationController#verify' ]); // throws an error
// Route::get( 'verify', [ 'as' => 'verify', 'uses' => 'ActivationController#create' ]); // this works ?!?
app/Http/Controllers/ActivationController.php
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ActivationController extends Controller {
// ....
public function verfiy( ) {
return "verify";
}
public function create()
{
return "create";
}
// ...
You misspelled the function. :)
public function verfiy( ) {
^^

How the laravel process it's routes?

I am studying Laravel 5 as my new framework and I am following the video in Laracast and I got some weird error. I am displaying simple view in my controller but all I got is this error:
ModelNotFoundException in Builder.php line 125: No query results for model [App\Article].
Here's a bit of my code:
Route:
Route::get('/', 'WelcomeController#index');
Route::get('home', 'HomeController#index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Route::get('articles', 'ArticlesController#index');
Route::get('articles/{id}', 'ArticlesController#show');
Route::get('articles/create', 'ArticlesController#create'); //returns error page
ArticlesController.php
<?php namespace App\Http\Controllers;
use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ArticlesController extends Controller {
public function index() {
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function show($id) {
$article = Article::findOrFail($id);
return view('articles.show', compact('article'));
}
public function create() {
return 'Hello World'; //display error messages
}
}
So I just confused because when I try to access the create() method the Laravel also read the show() method.
Is this correct? So in the route list the Laravel will read it's routes from top to bottom?
So in my route in order to prevent the error I should put first the route of create before the show?
So it should be like this?
Route::get('articles', 'ArticlesController#index');
Route::get('articles/create', 'ArticlesController#create'); //make it first?
Route::get('articles/{id}', 'ArticlesController#show');
ModelNotFoundException is a db exception fired from the findOrFail method if the model your trying to find don't exist
check this redirect-if-model-doesnt-exists-modelnotfoundexception-doesnt-work-for-me
Because the /create also triggers the show url /{any}
You should do what you said, put the create route over the show route, or delegate that boring job using
Route::resource ('article', 'ArticleController')
Therefore you are getting a modelNotFoundException, because you are looking for an article with id = 'create'

Laravel 5 routes file is rendering multiple views

I'm new to laravel and I'm trying to build a CMS with Laravel to learn it on the go. Now i've got this problem with my routes.
When I visit http://my.app/admin both the views dashboard.index and pages.page are getting loaded. I was under the impression that laravel handles routes in the order they are set in the routes file and if a route gets found everything after that doesn't get executed.
What am i doing wrong here? I'm using Laravel 5.
Routes file:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('/', array(
'as' => 'cms.dashboard',
'uses' => 'DashboardController#index'
));
});
Route::get('/{slug}', array(
'as' => 'pages.page',
'uses' => 'PagesController#page'
));
Controllers:
class DashboardController extends Controller {
public function index()
{
return view('dashboard.index');
}
}
class PagesController extends Controller {
public function page($slug)
{
return view('pages.page');
}
}
Found the problem and it had nothing to do with Laravel.. This was in a javascript file included in the dashboard.index view:
$.get("skin.html", function (data) {
$('body').append(data);
});

laravel 5 controller not visible via browser

I know laravel 5 isnt out yet and in development stages but I have been playing around with it to try and understand how it works. Using the HomeController, i added another method called contact and when i try to visit it via the browser it just show a 404 page. What am i doing wrong? By default routes are disabled and everything is passed through the controllers.
http://domain.com/home/contact
<?php namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
class HomeController extends Controller
{
public function index()
{
$data = array(
'fname' => 'sarmen',
'lname' => 'b'
);
return view('pages.home')->with('data', $data);
}
public function contact()
{
return 'contact us';
}
}
in
app/Providers/RouteServiceProviders.php
this line
require app_path('Http/routes.php');
is commented out. So i just uncommented it and put this into my routes.php
$router->get('contact', 'HomeController#contact');
and it still doesn't work.
You should put the solution here and also a link for reference, not only the link:
From Laracast forum:
You need to specify the full path:
App\Http\Controllers\HomeController
Either that, or add a namespace to the RouteServiceProvider section, where you require routes.php - like this:
public function map(Router $router) {
$router->group(['namespace' => $this->rootUrlNamespace],
function() use ($router) {
require app_path('Http/routes.php');
});
}
found the solution thanks to jeffrey way
https://laracasts.com/discuss/channels/general-discussion/controller-class-not-found

Categories