Laravel 5 routes file is rendering multiple views - php

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

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.

Generating URL specific routes in a multi-URL Laravel application

I have multiple URLs going to a single Laravel application:
www.mydomain.com
panel.mydomain.com
Within this I have several routes configured:
<?php
Route::group(['middleware' => ['web']], function () {
Route::get('/page', 'App\MyApp\Page\Controllers\PageController#index')->name('home');
});
Route::group(['middleware' => ['web'], 'domain' => 'panel.mydomain.com'], function() {
Route::get('/page', 'App\MyApp\Page\Controllers\ControlPanelPageController#index')->name('controlpanel.dashboard');
});
So anyone going on panel.mydomain.com gets a ControlPanelPageController index method, everyone else gets the PageController method.
However I'm having difficulty generating a link from a named route.
For example:
<?php
namespace App\MyApp\Page\Controllers;
use App\Http\Controllers\Controller;
class ControlPanelPageController extends Controller
{
public function index()
{
echo route('home');
// output: /page
echo url( route('home') );
// output: panel.mydomain.com/page
// required output: www.mydomain.com/page
}
}

Define Laravel 5 route inside subfolder and display it via controller

I have Laravel 5.2.45 app.
I have controller structure like this:
App
Http
Controllers
Admin
AdminController.php
inside AdminController.php I have
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
class AdminController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('is.admin');
}
public function index()
{
return view('admin.home');
}
}
I have views folder structure like this:
views
admin
home.blade.php
And inside my routes.php I have
Route::get('/admin/home', 'Admin\AdminController#index');
So I'm trying to get that when I type .../admin/home browser displays home.blade.php inside admin folder.
My routes.php:
Route::auth();
Route::get('/', 'FrontController#index');
Route::get('/home', 'FrontController#index');
Route::get('/add_user', 'FrontController#user');
Route::group(['prefix', 'admin', 'namespace' => 'Admin'], function() {
Route::get('home', 'AdminController#index');
});
The prefix is missing in your route definition. Correct it to look like this:
<?php
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
Route::get('/home', 'AdminController#index');
});
Now, try base_url/admin/home in your browser and it should work.
You can use route groups with the namespace and prefix options.
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
Route::get('home', 'AdminController#index');
});
Here, the prefix allows you to specify the beginning of a URL that should always be in the routes inside the group. So any routes you put inside that group should start with admin.
The namespace lets you specifiy a folder/namespace for the controllers you reference. So all the controllers must be in the App\Http\Controllers\Admin namespace and the app/Http/Controllers/Admin folder.
You need to drop the leading forward slash so it becomes:
Route::get('admin/home', 'Admin\AdminController#index');

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

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

Categories