Return to resource route - laravel

I have below route in my route file.
Route::group( ['prefix' => 'prayer', 'as' => 'prayer.'], function () {
Route::resource( 'time', 'PrayerTimeController' );
});
How can I return to this route from Controller ?

Firstly, I highly recommend reading through the excellent Laravel documentation: https://laravel.com/docs/9.x/controllers#resource-controllers
To get the route wired up to a controller, update your route resource declaration to use the full class name of the controller:
Route::resource('time', PrayerTimeController::class);
Create a controller file: App\Http\Controllers\PrayerTimeController.php with this content:
<?php
namespace App\Http\Controllers;
class PrayerTimeController extends Controller
{
public function index()
{
return 'Hello, World!';
}
}
Then import the controller namespace into the routes file you're working in, e.g. routes/web.php file:
use App\Http\Controllers\PrayerTimeController;
Route::group(['prefix' => 'prayer', 'as' => 'prayer.'], function () {
Route::resource('time', PrayerTimeController::class);
});

Related

Why there is an error with the resource controller?

I have an Admin folder with UserController (resource) inside
--Controllers
--Admin
-- UserController.php
In web.php I have the following routes:
use App\Http\Controllers\Admin\UserController;
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function (){
Route::get('/', [HomeController::class, 'index']);
Route::resource('user', UserController::class);
});
UserController
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index()
{
return view('admin.user.index');
}
}
When I try to go to admin/user I get an error
Target class [Admin\App\Http\Controllers\Admin\UserController] does not exist.
I will be grateful for the hint.
You set the namespace to Admin so it adds it in the base namespace. Just remove the namespace. It should work.
Route::group(['prefix' => 'admin'], function (){
Route::get('/', [HomeController::class, 'index']);
Route::resource('user', UserController::class);
});

How remove pages word in url - domain/pages/disability-support in laravel 5.6

my url:- domain/pages/disability-support
i need domain/disability-support
route :-
Route::any('/pages/{name}', 'PageController#pages')->name('pages');
You can to use an route parameter with page name. Like it:
Route:
Route::any('/{name}', 'ProfileController#pages')->name('pages');
Controller:
<?php
...
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function pages(Request $request, $name)
{
}
}
Route:
Route::any('/{name}', [
'as' => 'pages',
'uses' => 'ProfileController#pages',
]);
Controller:
class ProfileController extends Controller
{
public function pages($name)
{
}
}

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

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

Internal request without change the current route

How I can make a internal request without change the current route.
My routes:
Route::get('/my/action1', ['as' => 'action1', 'uses' => 'MyController#action1']);
Route::get('/my/action2', ['as' => 'action2', 'uses' => 'MyController#action2']);
My controller:
namespace App\Http\Controllers;
use Route;
use Request;
class MyController extends Controller
{
public function action1()
{
return 'action1';
}
public function action2()
{
$request = Request::create('/my/action1', 'GET');
$response = app()->handle($request);
dump(Route::getCurrentRoute()->getName()); // I need return "action2"
return $response; // I need return "action1"
}
}
When the internal request is called, the current route is changed and I don't want this effect. What I need make?

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

Resources