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
}
}
Related
In my web.php I have the following route set up. What I was wanting to know is there something specific that I need to follow to get a sub domain of a sub domain to work?
The domain I am using is blah.blah.domain.tld
web.php:
Route::group(['domain' => '{blah}.blah.domain.tld'], function (){
Route::get('', 'DealsFrontEnd#index' );
});
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class DealsFrontEnd extends Controller
{
public function index()
{
return view('front.deals');
}
}
Too long for a comment: Try using a closure for debugging:
Route::group(['domain' => '{blah}.blah.domain.tld'], function (){
Route::get('', function() {
echo "Hello World";
});
});
Make sure you have debug enabled to make use of Laravel's error handling/reporting.
As I said in the comments, you shouldn't wrap the subdomain in brackets unless you want it to be dynamic.
{blah}.blah means it will capture anything.blah and the route variable $blah will be equal to anything.
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');
I am using Zizaco/entrust laravel package as a ACL Manager for my project.
I know that for limit access to a route group via middlewares and assign a role (or permission) to it, I should do that like this:
Route::group(['prefix' => 'admin', 'middleware' => ['role:admin']], function() {
....
});
But I want to assign separate permission to different routes(methods) of a resource controller.
I know that how can so that for whole resource but I can not implement it for each controller method:
Route::group(['prefix' => 'admin', 'middleware' => ['role:admin']], function() {
Route::resource('/post', ['middleware' => ['permission:manage-posts'], 'uses' => 'PostController']);
});
I want to assing this permission to related method :
'post-create' => public function create ()
'post-edit' => public function edit()
and so on.
You can assign middlewares in your controller's constructor:
class Foo extends Conroller
{
public function __construct() {
$this->middleware('post-create', ['only' => ['create']]);
$this->middleware('post-edit', ['only' => ['edit']]);
}
}
Imagine you have apiResource units-of-measure. You can assign different middlewares to separate endpoints like this:
Route::middleware('role:seller|buyer')->group(function () {
Route::apiResource('units-of-measure', UnitOfMeasureController::class)->only('index');
});
Route::middleware('role:seller')->group(function () {
Route::apiResource('units-of-measure', UnitOfMeasureController::class)->except('index');
});
The index endpoint will be accessible for sellers as well as for buyers. The rest of endpoints are only for sellers.
you can chain the methods, using the only method.
here is an example:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Spatie\Permission\Models\Role;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('permission:read-user')->only('index','show');
$this->middleware('permission:edit-user')->only('edit','update');
$this->middleware('permission:delete-user')->only('delete');
$this->middleware('permission:create-user')->only('create','store');
}
I am trying to do something that I believe to be simple, I'm just having some trouble along the way. My code below includes my routes.php and my verify.php that authenticates a user. Basically, I want the routing to the homepage to change based on if the user is authenticated or not.
So first, I have the controller (verify.php)
<?php
namespace App\Http\Controllers;
use App\Email;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Google_Client;
use Auth;
use App\User;
class verify extends Controller
{
public function verifyIdToken(Request $request)
{
$user = User::where('name', 'Molly')->first();
Auth::login($user);
return;
}
}
This script is hit in an ajax request when the user first satisfies some conditions to be logged in, but that is client-side logic and not important at this point. The idea is that once the script is hit, the user is logged in.
To add more detail:
The header of my routes.php is this:
use Auth;
use Illuminate\Http\Request;
Is there something I am missing?
My routes.php logic is now this:
if (Auth::guest()) {
Route::get('/', function () {
return view('welcome');
});
} else {
Route::get('/', function () {
return view('mainview');
});
}
But it still doesn't work, even though in verify.php I have added in the following confirmation:
if (Auth::check($user))
{
return view('aviewII')->with(['verify' => json_encode("Yes!")]);
}
Which I am getting returned in my ajax requests.
So even though that tells me I am authenticated, I still can't get the authenticated view. What should I try next?
Edit: My routes.php in its entirety:
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
use App\punsmodel;
use Illuminate\Http\Request;
if (Auth::guest()) {
Route::get('/', function () {
return view('welcome');
});
} else {
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
return view('mainview');
});
Route::get('mainview', function () {
return view('mainviewMolly');
});
});
}
Route::get('puns', function () {
return view('puns');
});
Route::post('google' , [
'as' => 'verify.index',
'uses' => 'verify#verifyIdToken'
]);
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
//
});
I’d just have a conditional in routes.php:
if (Auth::guest()) {
Route::get('/', function () {
return view('home.logged_out');
});
} else {
Route::get('/', function () {
return view('home.logged_in');
});
}
Also, a couple of styling tips:
Classes should have “StudlyCased” names, so class verify should be class Verify.
Order your use statements alphabetically, makes scanning much easier ;)
UPDATE:
If you want to check the authentication status of a user, you need to make sure those routes are inside the group with middleware applied:
Route::group(['middleware' => 'web'], function () {
if (Auth::guest()) {
return view('home.logged_out');
} else {
return view('home.logged_in');
}
});
Ok so if you're on L5.2, this is a bit different. Check this article.
From what I got, you have to add an api_token field to your users table. Then when you want to check if a User is logged in, you can do this Auth::guard('api')->check().
When you log your user, you have to return the api_token in your response and add this field to any of your Ajax requests.
Hope that helped ;)
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);
});