NotFoundHttpException for Controller route in Laravel 4 - laravel

I am trying to get started using controllers in Laravel 4 and I am running into some trouble. Here is the basic run down:
I have a controller in the controllers folder called FansController in the file FansController.php:
<php
class FansController extends BaseController {
public $restful = true
public function getindex() {
return View::make('fans.landing');
}
}
Within my views folder, I have a folder called "fans" that controls a view file "landing.blade.php". It contains simple html: <h1>hello</h1>
In my routes.php file, I have a route calling the controller. Here is that code:
Route::get('landing', array('uses' => 'FansController#index'));
When I visit the url: public/fans/landing
I receive a "NotFoundHttpException";
Do you have any ideas what may be going wrong? Thank you for your help.

First off you need to specify what version of Laravel you are running.
If you are running Laravel 4 then:
// routes.php
Route::get('/', array('uses' => 'GuestController#getIndex'));
// GuestController.php
class GuestController extends BaseController {
public function getIndex() {
return 'Hello world.';
}
}
Then run $ composer dump-autoload -o or php composer.phar dump-autoload -o (if your composer is installed locally) on your CLI
In laravel 3, however
// routes.php
Route::get('/', array('uses' => 'GuestController#index'));
// GuestController.php
class GuestController extends BaseController {
public $restful = true;
public function get_index() {
return 'Hello world.';
}
}

Probably you need to change your method name to index:
class FansController extends BaseController {
public $restful = true
public function index() {
return View::make('fans.landing');
}
}

Related

How import data using controller to view in Laravel 8.*

I would like to go to specific object view from main page(index). Here is how I communicate with page which is about to be showed
{{ route('object',['id'=>$object->id]) }}
Error message:
Error message says that it should be something wrong with controller, but in previous case this code worked.
web.php(routes)
Route::get('/object/{id}','FrontendController#object')->name('object');
My FrontendController
use App\Interfaces\FrontendRepositoryInterface;
class FrontendController extends Controller
{
public function __construct(FrontendRepositoryInterface $frontendRepository)
{
$this->fR = $frontendRepository;
}
public function index()
{
$objects = $this->fR->getObjectsForMainPage();
//dd($objects);
return view('frontend.index',['objects'=>$objects]);
}
public function object($id)
{
$object = $this->fR->getObject($id);
//dd($objects);
return view('frontend.object',['object'=>$object]);
}
FrontendRepository
use App\Models\EventObject;
use App\Models\Photo;
use App\Interfaces\FrontendRepositoryInterface;
class FrontendRepository implements FrontendRepositoryInterface {
public function getObjectsForMainPage()
{
return EventObject::with(['photos','clubs'])->get();
}
public function getObject($id)
{
return EventObject::with(['photos','clubs'])->get();
}
}
Solution was to clear routes cache using php artisan route:cache and to add missing / in route
Route::get('/object/'.'{id}','FrontendController#object')->name('object');

Project Class 'App\Application\Model\Slider' not found after uploading

My project worked fine on localhost, but once it was uploaded to a Linux shared host, I got the following error.
Class 'App\Application\Model\Slider' not found
This problem occurred in three of my models: sections, projects, Slider. The other models work fine.
HomeController.php
namespace App\Application\Controllers;
use App\Application\Model\Page;
use App\Application\Model\projects;
use App\Application\Model\section;
use App\Application\Model\Slider;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth')->except(['getPageBySlug', 'welcome']);
}
public function index()
{
return view('website.home');
}
public function getPageBySlug($slug)
{
$page = Page::where('slug', $slug)->first();
if ($page) {
return view('website.page', compact('page'));
}
return redirect('404');
}
public function welcome()
{
$sections = \App\Application\Model\section::limit(3)->orderBy('id')->get();
$project = \App\Application\Model\projects::limit(3)->orderBy('id')->get();
$sliders = \App\Application\Model\Slider::get();
return view('website.welcome', compact('projects', 'sections', 'sliders'));
}
}
Namespaces are loaded through an autoload file. When you push to your shared hosting, you'll likely have to run 'composer dump-autoload' in the project root to compile this file.

Laravel Serviceprovider $this->loadViewsFrom doesn't load blade view wright from package

I am creating a Composer package, beginner
Via Composer I have added my package to test application.
The test application output should be 12345, but instead it prints view name
What is wrong?
DownloadsServiceProvider:
class DownloadsServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadViewsFrom(__DIR__.'/resources/views/download', 'downloads');
public function register()
{
new Routes();
}
}
Controller:
namespace xxx\xxx\downloads;
use Illuminate\Http\Request;
class DownloadsController
{
public function index(Request $request)
{
$test='12345';
return View('downloads::test' ,compact('test'));
}
}
Route:
public function registerRoutes()
{
Route::prefix(self::CMSURL)->group(function () {
Route::get(
'/downloads',
function (Request $request) {
//return (new DownloadsController())->index($request)->name(self::DOWNLOADS_LISTER);
return (new DownloadsController())->index($request);
}
);
});
}
View:
{{$test}}
12345
Output source:
downloads::test

Laravel 5 : php artisan route:list Illuminate\Container\BindingResolutionException]

When I use the command "php artisan route:list" I have this error
[Illuminate\Container\BindingResolutionException]
Unresolvable dependency resolving [Parameter #0 [ <required> $methods ]] in class Illuminate\Routing\Route
The command works nicely when I have
Route::get('/', 'WelcomeController#index');
Route::get('home', 'HomeController#index');
but as soon as i add other routes, I have this exception
I found the problem :
I'm doing IOC in the controller and I inject Illuminate\Routing\Route. As soon as i delete the injection its works.
I have BaseController which extends Controller.
The BaseController is
class BaseController extends Controller {
/**
* #var array Options used by the pages
*/
protected $options;
/**
* #param Route $route
*/
public function __construct(Route $route)
{
$this->options = Option::getAutoloaded();
// Load the options for the named route page
$this->options = array_merge($this->options, Option::getByPage($route->getAction()['as']));
$this->initListPlugins();
}
A controller is
class MediasController extends BaseController {
public function __construct(Route $route)
{
parent::__construct($route);
$this->options = array_merge($this->options, Option::getByPage('media'));
}
Now thre problem is how I fix it :)
Thanx for your help
When ever I see this error I think of typo's, so make sure you recheck everything for those.
Just make sure your Route is linked to the correct Controller#function:
Route::get('medias', ['uses' => 'MediasController#getMediaList', 'as' => 'medias.index'])
Would go to the MediasController in Http/Controllers:
class MediasController extends Controller {
public function getMediaList()
{
return view('medialist');
}
}
Also make sure your controller has a correct return to the view.
And if you're using the
{!! link_to_route('medias.index', 'Media list') !!}
make sure you have the "illuminate/html": "5.0.*#dev" installed through your composer.json aswell.

Laravel Controller Subfolder

I tried to get the following curl statement work.
curl -X GET http://localhost:8080/api/v1/admin/user
In routes.php
Route::resource('admin.user', 'admin/UserController');
Route::get('/admin/user/{userid}', 'UserController#getAdminUser');
Route::get('/admin/user', 'UserController#index');
controllers/admin/UserControler.php
<?php namespace Controllers\Admin;
class UserController extends \BaseController {
public function index()
{
return DB::table('users')->get();
}
public function getAdminUser($userid)
{
return DB::table('users')->where('userid', $userid)->get();
}
}
After I run the curl command, all list of user do not show up. Please give me advise.

Resources