Laravel 5.6 Custom method to call a view - php

I have application in laravel.When I try to access a custom method using URL localhost/blog/public/contact/myownview it doesn't give any output. I want to redirect it to view called video.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ContactController extends Controller
{
//My Custom method
public function myownview(){
echo "yest";
//return view('video');
}
}
routes/web.php
Route::get('/contact/myownview','ContactController#myownview');

Try this
Route::get('/my-own-view', 'ContactController#myownview')->name('my-own-view);
and hit the http://localhost:8000/my-own-view, <url>+route name
return view('video');
Make sure in resources/views file has video.blade.php

You need to custom your route.php
Route::get('/blog/public/contact/myownview','ContactController#myownview');

Related

problems with routes laravel

After data from a form is saved i wanted to get back to the admin page.
I checked the database and the new data was there but I got an error:
"Route [pages.admin] not defined."
My Admin Controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\Admin;
class AdminController extends Controller
public function store(Request $request)
{
// Validation code
// Saveing code
return redirect()->route('pages.admin')
->with('success', 'Admins created successfully.');
}
My Page Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controllerpublic
function admin(){
return view('pages.admin');
}
Routes:
Route::get('/admin', 'PagesController#admin');
Route::post('admin_form', 'AdminController#store');
Would appreciate the help.
I looked in online sources but it didn't help
You are confusing the name of a view with the name of a route. Your view has the name pages.admin because there is a admin.blade.php view in the pages folder within the views folder of your application.
For route('pages.admin') to work, you need to assign a name to a route. You may do this by using name() when defining your route.
Route::get('/admin', 'PagesController#admin')->name('pages.admin');
It is a good practise to always name routes. For example: it allows you to just change the url without having to worry about your redirects breaking, since they use the name that hasn't changed.
I found a video and changed my code in the controller to
return redirect('admin');
and it worked.

Call to undefined method Illuminate\Support\Facades\App::index()

I'm new to learning Laravel but I'm having trouble routing to controller, I have a controller named "App" and I have a function named index in it, it says it can't find it in "App" controller even though I set it in the route
Error
Error
Call to undefined method Illuminate\Support\Facades\App::index()
http://localhost:8000/anasayfa
App.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class App extends Controller
{
public function index(){
return "anasayfa";
}
}
web.php
Route::get('/anasayfa', 'App#index');
What is the reason for this error?
A class with the name App already exists in Laravel, defined in namespace Illuminate\Support\Facades\App
if you want to use your class make sure to add
use App\Http\Controllers\App
in your web.php
It is recommended to use a different name. You should follow the conventions and name it AppController.
I'm solved.
I deleted the controller named "App" and created a controller named "AppController". But this caused a new error, Laravel could not find class "AppController". For this I updated web.php as follows;
use App\Http\Controllers\AppController;
Route::get('/anasayfa', [AppController::class, 'index']);

how to route any user controller in '/' in laravel 8?

I am learning laravel and i saw people creating a route like
Route::get('/user','homeController#fetchSocialLinks');
the homeController has these code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class homeController extends Controller
{
public function fetchSocialLinks()
{
$test= DB::select('SELECT * FROM `social_links`');
return view('/',compact('test'));
}
}
i want to get the $test variable in ('/') i.e. my root address page .
Can any one tell me the right way to do it.
Thanks
When you use the view method, the first argument to pass is a page, i.e you have a blade page named welcome.blade.php, so that function fetchSocialLinks will render a view and you can pass to it a variable like the following code :
return view('welcome',['test'=>$test]);
Some good practices suggest you to name that method "index", concerning the routes you have to know that something changed since Laravel 7, you have to declare your controller like that :
use App\Http\Controllers\homeController;
Route::get('/', [homeController::class, 'fetchSocialLinks']);

Redirect action to controller in different namespace

I have middleware that uses a redirect to call a controller which then displays a view.
public function handle($request, Closure $next)
{
redirect()->action('Full\Namespace\To\Controller\ErrorController#fourOhThree');
}
I also have this as a route. When I follow the route the view is displayed fine. When I try and redirect using action and pass the namespace of my controller, laravel tries to find the controller in the base app. I get error
Action App\Http\Controllers\Full\Namespace\To\Controller\ErrorController#fourOhThree not defined.
When the controller is located at
App\Vendor\Myname\Mypackagename\Controllers\ErrorController#fourOhThree
I have namespaced my controller correctly as far as I can tell as it matches the other namespaced controllers in this directory. This is the only controller I am trying to call from an action.
ErrorController.php within App\Vendor\Myname\Mypackagename\Controllers
namespace Full\Namespace\To\Controller;
use App\Http\Controllers\Controller;
class ErrorController extends Controller
{
public function fourOhThree()
{
return view('...');
}
}
I think I am doing something wrong in how I am passing the namespaced controller to the action method.
Try adding a '\' in front of the qualified name.
action('\Full\Namespace\To\Controller\ErrorController#fourOhThree')

Laravel 5 registering a controller to map all methods

I am new to Laravel 5 coming from CodeIgniter background. I have habit to not play with routes.php. CodeIgniter automatically maps methods like controllerName/MethodName. But in Laravel 5 I am trying to do same by registering a controlller by writing this at top of app/http/sroutes.php:
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
When I run php artisan route:list it show that controller is registered. But when I see URL /public/admin/user/addRole it show addRole method not exist while I have created a method in AdminUserController.
Admin/AdminUserController.php
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getaddRole(){
echo "adding Roles";
}
}
Routes.php
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getAddRole(){
echo "adding Roles";
}
}
NB: Notice getAddRole() not getaddRole(), use camelCase
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI like this:
public/admin/user/add-role
It's hard to tell because I don't see your controller code but I assume you missed adding a HTTP verb to the method name. Like:
public function getAddRole(){
// ...
}
If you want the method to match any request method, use any:
public function anyAddRole(){
// ...
}

Categories