Laravel 5 named route to controller action - php

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( ) {
^^

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.

How to choose a Controller in the routes

In Laravel 4.2, I have the following route:
Route::group(array('before' => 'auth'), function() {
Route::post('/account/edit', array(
'as' => 'account-edit',
'uses' => 'UserController#accEdit'
));
});
I have a ClientController and an AdminController for common user and admin, respectively.
Assuming that I know the user type (Auth::getUser()->getType()), how can I replace the UserController with the correct controller without adding extra logic to routes class? Can this be done with filters?
I'm trying to avoid an extra controller between the routes and the final controller.
Actually, it is not necessary to create two user controller. Just use middleware to limit the access rights of clients. By this way, you can keep the original UserController.
You can add IsAdmin.php in the middleware.
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Contracts\Auth\Guard;
class IsAdmin {
public function handle($request, Closure $next)
{
if (Auth::getUser()->getType() === 'admin')
{
return $next($request);
}
return new RedirectResponse(url('/'));
}
}
In kernel.php, you need declare your middleware.
protected $routeMiddleware = [
// some other middlewares
'admin' => 'App\Http\Middleware\IsAdmin',
];
Then, add the following statements in public function __construct of the UserController.php
$this->middleware('admin', ['only' => ['OnlyForAdmin1','OnlyForAdmin2']]);
Thus, clients will have no access to the function OnlyForAdmin1 and function OnlyForAdmin2.

simple Laravel View::make() not working

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');
}
}

Custom urls same controller

Suppose I had a controller that look like this:
AController.php
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class AController extends Controller {
public function doThis(){...}
public function doThat(){...}
public function doThing(){...}
}
routes.php
Route::get('/doThis', [
'as' => 'acontroller.dothis', 'uses' => 'AController#doThis'
]);
Route::get('/doThis', [
'as' => 'acontroller.dothat', 'uses' => 'AController#doThat'
]);
Route::get('/doThis', [
'as' => 'acontroller.dothing', 'uses' => 'AController#doThing'
]);
Is there a better way than using Route::get()? I want my route to be automatically ControllerName.methodName and the url to be /methodName without having to explicitly use Route::get()
You're looking for an "implicit controller" (docs here).
If you define your route like:
Route::controller('/', 'AController');
All of the routes underneath the specified prefix (first parameter) will get routed to that controller. Laravel then expects the method names to be defined as a combination of the HTTP verb and the route.
So, your controller would be:
class AController extends Controller {
public function getDoThis(){...} // GET to /doThis
public function postDoThat(){...} // POST to /doThat
public function anyDoThing(){...} // any verb to /doThing
}

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);
});

Categories