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
Related
I am fresher in cakephp. For my current project, I am using CakePHP skeleton app. Everything going fine. But when I am creating new controller for admin panel then it showing this message Did you really think you are allowed to see that?. Someone please help me.
I am showing my codes below:
Route:
Router::prefix('admin', function ($routes) {
// Other routes are here.
$routes->connect('/sections', ['controller' => 'Sections', 'action' =>'index']);
}
SectionsController.php
<?php
namespace App\Controller\Admin;
use App\Controller\AppController;
class SectionsController extends AppController {
public function index() {
echo "I am for sections page";
}
}
This controller is locate in src\Controller\Admin folder
Below is my error message.
probably this would be the solution.
use Cake\Event\Event;
class YourController extends AppController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow('index');
}
}
I am new in Laravel. I am trying to create a new page named as "contact". But i am getting a Object not found error when i am trying to access the contact page
URL: project-name/contact
please help me
---routes file
<?php
Route::get('/','WelcomeController#index');
Route::get('contact','WelcomeController#contact');
Route::group(['middleware' => ['web']], function () {
//
});
--- Welcome controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class WelcomeController extends Controller
{
public function index(){
return view('welcome');
}
public function contact(){
return 'Contact page goes here...';
}
}
Set your home directory to the public to make things work.
Exchange the route. like this:
Route::get('contact','WelcomeController#contact');
Route::get('/','WelcomeController#index');
Laravel 5.1
This seems strange to me:
Route::group([
'middleware'=>['auth','acl:view activity dashboard'],
'prefix' => 'api/v1'
], function(){
Route::controller('investment-transactions', 'Api\V1\Investments\InvestmentTransactionsController');
Route::controller('investment-transactions/{offeringID}', 'Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering');
});
Seems pretty normal to me, the controller:
namespace App\Http\Controllers\Api\V1\Investments;
use App\Brewster\Models\Company;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class InvestmentTransactionsController extends Controller {
public function __construct() {
}
public function getIndex() {
echo 'Here';
}
public function getTransactionsForOffering($offeringID) {
echo $offeringID;
}
}
Ok so the action and the controller do exit, but when I run: php artisan routes:list I get:
[ReflectionException]
Class App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering does not exist
Well obviously App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering is not a class, how ever: App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController is and getTransactionsForOffering is an action.
Whats going on?
I believe your problem is in the routes.php we can use controllers as follows
Route::get('investment-transactions', 'InvestmentTransactionsController#index');
Route::get('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
By default, our controllers are stored in App/http/controllers folder and laravel know it.
I believe you only need to reference the Class like so:
Route::controller('investment-transactions','InvestmentTransactionsController#Index'); //make sure you create a function for the index
Route::controller('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
Assuming you need to show a view for the route investment-transactions create the following function in your controller:
public function index()
{
return view('name-of-your-view-file');
}
Let me try to explain this.
If a guest goes to the / directory, he is welcome. If the guest tries to go to the /start directory, he is redirected to the log in page.
After a guest logs in, he is redirected to the /start directory. If a logged in user goes to the / directory, he is again redirected to the /start directory. (no need to see home page once logged in).
I got it working how I want, however I'm not sure if I'm doing this how Laravel intends for it to be done being that there is some non-DRY code in my PagesController.php. Plus, I'm basically redirecting the PagesController to the PagesController (doesn't sound like that follows good practices to me).
Here is the routes.php:
<?php
Route::get('', 'PagesController#index');
Route::get('start', 'PagesController#start');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController'
]);
Here is the PagesController.php:
<?php
namespace App\Http\Controllers;
use App\HowItWorksModel;
use App\WhatYouGetModel;
use App\StartContentModel;
use Illuminate\Http\Request;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PagesController extends Controller
{
public function index()
{
if (Auth::check()) {
return redirect()->action('PagesController#start');
}
$howItWorksContent = HowItWorksModel::all();
$whatYouGetContent = WhatYouGetModel::all();
return view('pages.index', compact(
'howItWorksContent',
'whatYouGetContent'
));
}
public function start()
{
if (Auth::check()) {
$startContent = StartContentModel::all();
return view('pages.start', compact(
'startContent'
));
}
return redirect('/auth/login');
}
}
How can I restructure this to work exactly as it works now but having better practices in mind? Or is what I'm doing perfectly okay for these purposes?
It is better done with middlewares, using middlewares will take the authentication resposability of your controllers and let it worry about content, this will make your controllers light and maintainable.
You should use Auth Middleware
You could try something like this. Not much different than what you had but does result in less code.
<?php
namespace App\Http\Controllers;
use App\HowItWorksModel;
use App\WhatYouGetModel;
use App\StartContentModel;
use Illuminate\Http\Request;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PagesController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => 'index']);
}
public function index()
{
if (Auth::check())
return redirect('start');
$howItWorksContent = HowItWorksModel::all();
$whatYouGetContent = WhatYouGetModel::all();
return view('pages.index', compact(
'howItWorksContent',
'whatYouGetContent'
));
}
public function start()
{
$startContent = StartContentModel::all();
return view('pages.start', compact(
'startContent'
}
}
I'm new to programming. I've been using codeigniter, and Laravel seemed a classy framework to learn.
I have this routes:
Route::get('slider', 'AdminController#getSlider');
Route::get('/', 'AdminController#getSlider');
and my controller:
class AdminController extends BaseController {
public function getSlider(){
return View::make('list', array('section' => 'slider_home'));
}
}
"public/" works great. "public/slider" spits NotFoundHttpException.
If there is some data that i should add please tell me.
Thanks, and excuse my english.
UPDATE:
I got it wrong. The correct URL was "public/index.php/slider". Now I need to modify the .htacces to change that.
Try this Route :
Route::controller('','AdminController');
Controller :
class AdminController extends BaseController {
public function getIndex(){
return View::make('list', array('section' => 'slider_home'));
}
public function getSlider(){
return View::make('list', array('section' => 'slider_home'));
}
}
ok you can use public/ and public/slider