Laravel : How to define route start with prefix # like medium.com/#{username}? - laravel

How to define route in laravel , which start with prefix # in web.php
like myapp.com/#username

use route group() to define prefix as -
Route::group(['prefix' => '#username'], function () {
Route::get('/','TestController#test');
});
You can access this url as www.base_url.com/#username/
If you want to set username dynamically then you could this-
Route::group(['prefix' => '#{username}'], function () {
Route::get('/','TestController#test');
});
And in your controller-
public function test($username)
{
echo $username;
}

I have got my answer .
Route::group(['namespace' => 'User','prefix' => '#{username}'],
function () {
Route::get('/','UserController#get_user_profile');
});
in User Controller
public function get_user_profile($username){
$user=User::where('slug',$username)->first();
return response()->json($user);
}

Related

if there an function to handle

All of my routes have a lang parameter and I need to unset it in the controller. How can I achieve this?
routes.php
Route::prefix('{lang?}/admin')->attribute('namespace','Admin')->middleware('auth:web')->group(function () {
Route::get('/branch/{branch}/products/create', ['uses' => 'BranchesController#createBranchProduct', 'as' => 'admin.branch.products.create']);
});
Controller:
public function createBranchProduct(Branch $branch)
{
$categories = Category::all();
return View::make('admin.branches.products.new',['branch' => $branch,'categories'=>$categories]);
}
I'm getting the following error:
Hello turky eltahawy and welcome to StackOverflow!
Let's take a look: you have grouped routes which have an optional parameter. Therefore, when you are calling createBranchProduct method, it expects two parameters: lang and instance/id of the Branch model.
What you can do is to accept 2 parameters in the createBranchProduct like this:
public function createBranchProduct($lang = null, Branch $branch)
{
$categories = Category::all();
return View::make('admin.branches.products.new',['branch' => $branch,'categories'=>$categories]);
}
I found an answer that I can make:
class baseController extends Controller {
public function callAction($method, $parameters){
unset($parameters['lang']);
return parent::callAction($method, $parameters); //TODO: Change the autogenerated stub
}
}

Laravel redirect route with a parameter from controller

I'm trying to redirect a route from a controller function after a form submit process in Laravel 5.4 as it is said in link below
https://laravel.com/docs/5.4/redirects#redirecting-named-routes
Route;
Route::group(['middleware' => ['api'], 'prefix' => 'api'], function () {
Route::post('doSomething', 'Page#doSomething');
});
Route::post('/profile', function () {
//..
});
Controller;
public function doSomething(Request $request){
return redirect()->route('profile', ['id'=>1]);
}
When I try to redirect I get this error.
InvalidArgumentException in UrlGenerator.php line 304: Route [profile]
not defined.
I have searched several times about redirection but I got similar results.
Any suggestions ? Thank you.
route() works with named routes, so name your route
Route::post('/profile', function () {
//..
})->name('profile');

Is it possible to use prefix as param ir Laravel routes?

I have different languages on the page and I'm wondering is it possible to use them as prefix param. Something like this:
Route::group(['prefix' => '{lang}'], function() {
Route::get('/', 'BlogController#posts');
})->where('lang', '(en|fr|de)');
The closest i could think of was this:
Route::group(['prefix' => '{lang}'], function() {
Route::get('/', 'BlogController#posts')->where('lang', '(en|fr|de)');
});
This code get language from your languages table. Hope you can change this code to your requirements, if not - ask ;) try to help you..
Model
class Language() {
public $table = 'languages';
public $timestamps = false;
public function set() {
$code = Request::segment(1);
$language = Language::whereCode($code)->first();
return $language;
}
Router
$language = new Language();
$language->set();
Route::get('/', function() use ($language) {
return Redirect::to('/' . $language->code);
});
Route::group(array('prefix' => $language->code), function() {
Route::get('/', array('as' => 'home', 'uses' => 'PageController#index'));
});

laravel 4 page is not working for a newbie

I am totally new for laravel and trying to create a view for about us page.
But it is not working right when I am setting this in a route file.
Here is my HomeController functions:
public function showWelcome()
{
return View::make('hello');
}
public function showAboutus()
{
return View::make('about.about');
}
And here is the routes file:
Route::get('/', 'HomeController#showWelcome');
Route::get('about', 'HomeController#showWelcome');
.... 'HomeController#showWelcome' ...
should be
.... 'HomeController#showAboutus' ...
On the About route.
Plus the second parameter is an array so use
Route::get('/', array('uses' => 'HomeController#showWelcome'));
Route::get('about', array('uses' => 'HomeController#showAboutus'));

How can I redirect a route using laravel?

I use group to help visualize my project's routes.php, now the problem is
for example, when a user access to "/dir", I want to make it be redirected to "/dir/ele"
Route::group(['prefix' => 'dir'], function () {
Route::group(['prefix' => 'ele'], function () {
Route::controller('/', 'dir\eleController');
});
Redirect::route('ele');
}
Why is this not working?
The route dir/ele is going to a controller, but you are doing the redirect in your routes.php instead of in the controller.
You should use a closure route and do everything in the routes.php or use a controller and move the redirect to the controller:
Closure route in routes.php:
Route::group(['prefix' => 'dir'], function () {
Route::group(['prefix' => 'ele'], function () {
Route::get('/', function() {
return Redirect::to('ele');
});
});
});
Which can be simplified to:
Route::group(['prefix' => 'dir'], function () {
Route::get('ele', function(){
return Redirect::to('ele');
});
});
Or use the controller way:
Routes.php
Route::group(['prefix' => 'dir'], function () {
Route::group(['prefix' => 'ele'], function () {
Route::controller('/', 'eleController#redirect');
});
}
eleController.php
class eleController extends BaseController {
function redirect() {
return Redirect::to('ele');
}
}

Resources