Laravel 5.5 Routing and Sub-domain Routing - php

I am very new in Laravel. I currently created my personal site on Laravel 5.5 and uploaded to GoDaddy server: http://bhattraideb.com/. Now when I click on 'Blog' from navigation I would like to redirect like http://blog.bhattraideb.com/ which is currently redirection to 'bhattraideb.com/public/blog'.
Again from the same this link 'bhattraideb.com/public/blog' when I click on 'Resume' I aspect to redirect on 'bhattraideb.com/'
Here is my route code
//** Resume Routes
Route::prefix('/')->group(function() {
Route::get('/', 'Frontend\ResumeController#index')->name('resume.index');
Route::resource('resume', 'Frontend\ResumeController');
});
//** Blog Routes
Route::get('show/{id}', 'Frontend\PostController#show')->name('post.show');
Route::get('blog/{slug}', ['as' => 'post.single', 'uses' => 'Frontend\PostController#getSingle'])->where('slug', '[\w\d-\_]+');
Route::resource('blog', 'Frontend\PostController');
Can anyone please guide me for this.
Thanks in advance.

You can read up on https://laravel.com/docs/5.5/routing#route-group-sub-domain-routing for more details on this but the following simple example should work:
web.php
Route::group(["domain" => "blog.bhattraideb.com" ], function () {
Route::get("/", "Frontend\PostController")->name("blog.index");
//All blog routes should be defined in here
});
You can then use the helper route("blog.index") to get the URL along with the subdomain when generating links to the blog.
Note that you will need to setup the webserver to accept blog.bhattraideb.com as an alias of bhattraideb.com. Laravel will then sort the rest out

Related

Laravel, same path on a different subdomain uses wrong controller

Question
How can I set up Laravel routing so that:
navigating to mysite.com/login uses the LoginController
navigating to somecompany.mysite.com/login uses the TenantLoginController
What I'm doing
I'd have a Laravel 5.7 app that has a typical login page at say, mystite.com/login
I'd like to set up a subdomain for this app like somecompany.mysite.com that will have it's own authentication.
I'd like the somecompany users to log in at somecompany.mysite.com/login
What I've tried
The route definition for the main site login
Route::group(['namespace' => 'App\Http\Controllers\Auth', 'middleware' => ['web']], function () {
Route::get('login', 'LoginController#showLoginForm')->name('login');
});
The rout definition for the subsomain login
Route::domain('somecompany.mysite.com')->group(function ($router) {
$router->group(['namespace' => 'App\Http\Controllers\Tenant\Auth', 'middleware' => ['web']], function($router) {
$router->get('login', 'TenantLoginController#showLoginForm')->name('somecompany.login');
});
});
What Happened
I can navigate to somecompany.mysite.com/login and the URL bar says somecompany.mysite.com/login but when I do, the request is actually routed to the 'LoginController#showLoginForm' controller not the expected 'TenantLoginController#showLoginForm' and the typical login form is desplayed, not the subdomain's login form.
If I change the path to $router->get('tenant-login' and navigate to somecompany.mysite.com/tenant-login the subdomain login form is shown, and somecompany.mysite.com/login shows the main login form.
Since you did not specify a domain in the first route (handled by LoginController), it should also be valid for the somecompany.mysite.com subdomain.
To work around that, I would suggest trying to add more specificity to that first route, enclosing it with Route::domain('mysite.com').
The Laravel router always takes the first matching route, and that first one matches just fine in the end.

Laravel localization with Voyager admin

I'm making a Laravel website with Voyager admin and now I need to add localization to that website. Voyager includes its own translations table and I'm using it for creating content in multiple languages from backend. But in the frontend routing is not working, I'm getting NotFoundHttpException error. My routes are as following:
Route::group(['prefix' => 'fr'], function()
{
App::setLocale('fr');
Route::get('{slug}', 'PageController#show');
Route::get('posts/{slug}', 'PostsController#show');
//and so on
});
How can I fix this?
According to the documentation you can go
Route::prefix('admin')->group so
Route::prefix('fr')->group(function()
{
App::setLocale('fr');
Route::get('{slug}', 'PageController#show');
Route::get('posts/{slug}', 'PostsController#show');
//and so on
});
AND also the order is important.

Create new folder from public and use as a admin side in laravel

I'm new to laravel so if my question is not up to the mark then forgive me but I googled it and not found answer as per my requirement.I follow https://laracasts.com/discuss/channels/laravel/setup-different-frontend-backend-endpoints-in-laravel-51 && https://laracasts.com/discuss/channels/general-discussion/splitting-admin-and-front-end
MY NEED :
I want 2 entry point for my laravel application. one is for front user and second for admin user.
I am using laravel latest version (5.3.26).
As we know public folder is the entry point for laravel application.
Ex : http://localhost/news_report_old/public/index.php
Using above URl we can enter into application. Right ?
Now I want to create new entry point for Admin So I thing I can copy public folder and rename it so my work is done but I missing something and I can't figure out what I am missing ?
EX : http://localhost/news_report_old/admin/index.php
So when I open above URL I want to open Admin side.
How can I achieve this ?
Thanks.
You, don't need to copy public folder. That is terrific idea. (Don't ever mess with core/structure framework code, unless you have no any other option)
You can distinguish front end and admin panel easily using router and middleware.
You should create a custom middelware:
https://laravel.com/docs/5.3/middleware
And routing can be like:
routes/web.php:
// List of all front end routes:
Route::get('/', 'ArticleController#index');
Route::get('/home', 'HomeController#index');
Route::get('/about', 'HomeController#about');
// List of all admin routes
Route::group(['prefix' => 'admin', 'middleware' => 'admin']], function () {
Route::resource('admin', 'AdminController')
});

Laravel - All route links are prefixed with sub-domain

I'm using a sub-domain in my application as a dashboard for a user account. In the views of the dashboard, I include a master template which contains links in the navigation that lead to the root of the website. When I visited the sub-domain I've noticed all links in the navigation were changed based on the sub-domain name.
In my master template, links are generated using the helper function route(). When a generated link is viewed on my sub-domain, the link changes from domain.com/about to sub.domain.com/about along with all other links.
Route::group(['domain' => 'dashboard.project.app', 'before' => 'auth'], function()
{
Route::controller('/', 'DashboardController');
});
Route::get('/', ['as' => 'home', 'uses' => 'HomeController#index']);
Route::resource('products', 'ProductsController');
Route::resource('support', 'SupportController');
So visiting dashboard.project.app would trigger getIndex() in the DashboardController while visiting project.app would trigger index() in the HomeController.
Great, we've got this far. Now I have a simple view I use as a template that will contain URLs to the named resource routes defined outside of the sub-domain.
Again, URLs are made using the helper function route(). In a template extended by a view called in DashboardController I would have a link in the navigation like:
Products
which would generate Products as desired however changes to Products when shown in a view under the sub-domain.
So I would like the desired output to always be project.app/products and never dashboard.project.app/products as visiting that link will result in a 404 as there is no getProducts() method in my DashboardController. It's the wrong links!
Get what I'm saying?
If the named route is not defined as being specific to a domain, then the url generator will use whatever domain you are currently on. If the route should be specific to a domain, specify it:
Route::group(['domain' => 'project.app'], function() {
Route::resource('products', 'ProductsController');
Route::resource('support', 'SupportController');
});
Now, even from your dashboard, route('products.index') should give you project.app/products.

Admin Routes (or Prefix Routes) in Laravel 4

How can I create admin specific routes in Laravel 4 (Restfull Controllers):
/admin/users (get - /admin/users/index)
/admin/users/create (get)
/admin/users/store (post)
I want to know:
What Files and where I need create theam
How I need create the route
In Laravel 4 you can now use prefix:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#home');
Route::get('posts', 'AdminController#showPosts');
Route::get('another', function() {
return 'Another routing';
});
Route::get('foo', function() {
return Response::make('BARRRRR', 200);
});
Route::get('bazz', function() {
return View::make('bazztemplate');
});
});
For your subfolders, as I answer here "route-to-controller-in-subfolder-not-working-in-laravel-4", seems to have no "friendly" solution in this laravel 4 beta.
#Aran, if you make it working fine, please add an code sample of your controller, route, and composer.json files :
Route::resource('admin/users', 'admin.Users');
or
Route::resource('admin', 'admin.Users');
thanks
Really useful tool that you can use is the artisan CLI.
Using this you'll be able to generate the needed function file with all the required routes for it to become RESTful.
php artisan controller:make users
Would generate the function file for you. Then in your routes.php file you can simply add
Route::resource('users', 'Users');
This'll setup all the necessary routes.
For more information on this you should read the documentation at.
http://four.laravel.com/docs/routing#resource-controllers
http://four.laravel.com/docs/artisan
Edit:
To make this admin specific, simple alter the code like follows and move the controller to a admin folder inside the controllers folder.
Route::resource('admin/users', 'admin.Users');
The first paramater is the route, the second is the controller filename/folder.
In Laravel if you placed a controller inside a folder, to specific it in a route or URL you'd use the a dot for folders.
You can then expand on this and add Authentication using Route Filters and specifically the code found "Pattern Based Filters" found on the page below.
http://four.laravel.com/docs/routing#route-filters
Laravel 4 - Add Admin Controller Easily
This was driving me insane for ages, but i worked it out.
routes.php
Route::resource('admin', 'Admin_HomeController#showIndex');
/controllers/Admin/HomeController.php
Notice the folder name Admin must be captital 'A'
<?php
class Admin_HomeController extends Controller {
public function showIndex() {
return 'Yes it works!';
}
}
Alternatively you can use the group method
Route::group(array('prefix' => 'admin'), function() {
Route::get('/', 'Admin_HomeController#showIndex');
});
Thanks
Daniel

Categories