I am new to laravel4 .. I've made a simple website but when accessing SectionController or CompaniesController it redirects me to the SryoController while in other it works !!
that's my Route file
<?php
Route::get('/',array('uses'=>'SryoController#getIndex'));
Route::controller('admin/sections' ,'SectionsController');
Route::controller('admin/companies' , 'CompaniesController');
Route::controller('portfolio' , 'PortfolioController');
Route::controller('about' , 'AboutController');
Route::controller('contact' , 'ContactController');
?>
that's my getIndex() function in SectionController
public function getIndex () {
return View::make('sections.index')
->with('sections' , Section::all());
}
The order makes difference, try something like this, just to see what happens:
Route::controller('admin/sections' ,'SectionsController');
Route::controller('admin/companies' , 'CompaniesController');
Route::controller('portfolio' , 'PortfolioController');
Route::controller('about' , 'AboutController');
Route::controller('contact' , 'ContactController');
Route::get('/',array('uses'=>'SryoController#getIndex'));
Check your urls, the routes are correct, and you might have something in the filters.php that performs the redirect, or maybe have you changed your 404 view
Related
I just want to echo Hello world through Route
Route::get('/user',[addnewcontroller::class, 'getData']);
this is my web.php Route
and this is controller
class addnewcontroller extends Controller
{
function getData()
{
echo "My name is hassan";
}
}
whenever i try to execute it say controller doesnt exist, kindly let me know why im facing this error, ive tried many things like clearing caches, code etc
you need to define path of controller in web.php
like this:
use App\Http\Controllers\addnewcontroller;
You must use App\Http\Controllers\addnewcontroller at the top of routes
I am trying to add a new route to my application and can't seem to get it to work. I keep getting a 404 error. It looks like the physical path is looking at the wrong directory. Currently looking at D:\Web\FormMapper\blog\public\forms but should be looking at D:\Web\FormMapper\blog\resources\view\layout\pages\forms.blade.php
My request URL:
http://localhost/FormMapper/ /works fine
http://localhost/FormMapper/forms /doesn't work
http://localhost/FormMapper/forms.php /No input file specified.
my FormsController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormsController extends Controller
{
public function index()
{
return view('layouts.pages.forms');
}
}
My web.php:
Route::get('/', function () {
return view('layouts/pages/login');
});
Route::get('/forms', 'FormsController#index');
My folder structure looks like this:
My config/view.php
return [
'paths' => [
resource_path('views'),
],
'compiled' => env(
'VIEW_COMPILED_PATH',
realpath(storage_path('framework/views'))
),
];
you must use dot for this. In your controller change to this:
return view('layouts.pages.forms');
If your route only needs to return a view, you may use the Route::view method. Like the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:
Route::view('/', 'layouts.pages.login');
Route::view('/forms', 'layouts.pages.forms', ['foo' => 'bar']);
Check docs
After tracking digging deeper I determined that the issue was that IIS requires URL rewrite rules in place for Laravel to work properly. The index.php and '/' route would work b/c it was the default page but any other pages wouldn't. To test this I used the
php artisan serve
approach to it. and everything worked properly. Unfortunately I am unable to do this in production so I needed to get it to work with IIS.
When I create a new function in the controller for some reason it does not work. When I set the code from this function(getUnitsNotIn) to another function(index), that code works.
Does anyone know why this is happening?
My UnitsController.php action
public function index(){
$items = Unit::select('parent_id')->where('parent_id','!=',NULL)->get()->toArray();
return Units::whereNotIn('id',$items)->get();
}
public function getUnitsNotIn(){
$items = Unit::select('parent_id')->where('parent_id','!=',NULL)->get()->toArray();
return Units::whereNotIn('id',$items)->get();
}
My api.php
Route::apiResource('/units', 'UnitController');
Route::get('/units/notIn', 'UnitController#getUnitsNotIn');
In short, any new controller function that I make will not work. I tried to make a new controller and the same thing happens.
How to fix this problem?
It is not working because of apiResource(). Resource route assumes /units/{id}. so when you call /units/notIn route assume notIn as id And Action call show()
You need to use a different name.
Route::get('/un/notIn', 'UnitController#getUnitsNotIn');
Verb, path , action , route name
GET /units/{id} show units.show
Change this to
Route::get('/units-notIn', 'UnitController#getUnitsNotIn');
You define a resource controller so here in units/notin,notin define a id so
it call your show function default.
I'm using Codeigniter. Basically what I want is to remove the Controller name (Home) from the url.
Those urls look like:
http://localhost/mechanicly/Home
http://localhost/mechanicly/Home/about
http://localhost/mechanicly/Home/contactus
now there are two ways I can remove the Home controller:
static definition of every url inside route:
$route['about'] = "Home/about";
$route['contactus'] = "Home/contactus";
I can use call backs in routes in Codeigniter:
$route['(.+)'] = function ( $param ) {
if( strpos( $param, "Admin" ) !== false ) {
echo $param;
return "Admin/".$param;
}
else{
echo $param;
return "Home/".$param;
}
};
this logic is much better as it is generic and I don't have to create new routes every time for new method inside the controller.
It is working fine for the client controller which is Home but I have another controller named as Admin and I want to redirect Admin requests to the Admin controller and Home request to the Home Controller.
Why does above code work fine for the Home controller but returns
not found
when I validate for the Admin controller?
I am using CI version 3.x
If you really want to get crazy, you could parse the methods from the controller file and programatically create the "static" approach.
Pseudo code here
$controller_file_contents = file_get_contents('path/to/controller.php');
$controller_methods = function_that_parses_methods_from_file($controller_file_contents);
foreach ($controller_methods as $controller_method) {
$route[$controller_method] = "Home/" . $controller_method;
}
How function_that_parses_methods_from_file works is probably gonna involve a regex, something like function \w+. If you go with this approach, try to keep the controller as small as possible by offloading as much logic as possible into models, which is often a good idea anyways. That way the performance impact in the router is as small as possible.
Alternatively, you may be able to parse the controller using get_class_methods if you can figure out how to load the controller into memory inside the router without conflicting when you need to load the controller using the router or causing too much performance issues.
Pretty goofy, but every method you create in that controller will automatically create a route.
you can create your menu(urlĀ“s) from db like
tbl_menu tbl_level
---------- -------------
id id
fk_level level
name dateUP
dateUP active
active
In your controllers you need to call the correct menu by session or wherever you want
then you can has this in your route.php
$route['(.+)'] = 'int_rout/routing/' . json_encode($1);
in your controller Int_rout.php
public function routing ( $param ) {
$routing = json_decode($param);
$routing = explode('/', $routing);
//$menu -> get menu from model
foreach($menu as $item){
if($routing[0] === $item->name){
//$level -> get level from model
$redirect = $level->level;
}
}
//the final redirect will be like
//admin/user or admin/user/12
//public/us
$params = ( empty($routing[1])) ? '' : '/' . $routing[1];
redirect($redirect . '/' . $routing[0] . $params, 'refresh');
}
I have custom page in view/stock/three.php ( which I make) , I render it in StockController , and I try to open it from view/site/shop.php with
<div class="pager ">3</div>
but I get Not Found (#404) Page not found. . ( I didnt forget to set "use Url")
I try also to redirect it from SiteController with :
public function actionThree() {
return $this->redirect(['/stock/three']);
}
but also didnt work and still got Error code 404
maybe you don't have a 'three.php' file inside views/stock folder or that forward slash ('/') before 'stock'.
Try this
public function actionThree() {
return $this->redirect(['stock/three']);
}
I make new Model and new Controller for pages where I write :
public function actionThree() {
return $this->render('three');
}
and then
<div class="pager ">3</div>
work