I have controller Post with methods index, add and delete. I create routes rule for this actions:
$route['posts'] = 'post/index'; // <-- Work
$route['post-add'] = 'post/add';// <-- Work
$route['post-delete/(:num)'] = 'post/delete/$1'; // <-- Not Work
First route www.example.com/posts work good, and second with /post-add work. But when i call post-delete/5 that not work, all time i get 404 Not Fount.
Here is controller
class Post extends MY_Controller
{
public function index()
{
//.
}
public function add()
{
//.
}
public function delete($id)
{
echo "Delete post #ID =" $id;
}
}
I have only problem with delete route i dont know whay he dont accept that parameter.
Work:
www.example.com/posts
www.example.com/add
Not work:
www.example.com/delete/1
What can be problem?
Add it to route file
$route['delete/(:num)'] = 'post/delete/$1'
Related
I'm trying to set up a basic Laravel CRUD application, and I'm getting stuck setting up the pages for each action.
When I visit the route case/create, it opens the page for show instead.
routes/web.php
use App\Http\Controllers\HospitalCase as HospitalCase;
Route::controller(HospitalCase::class)->group(function() {
Route::get('/cases','index')->middleware('auth')->name('cases');
Route::get('/case/{id}','show')->middleware('auth');
Route::get('/case/create','create')->middleware('auth');
Route::post('/case/create','store')->middleware('auth');
Route::get('/case/edit/{$id}','edit')->middleware('auth');
Route::post('/case/edit/{$id}','update')->middleware('auth');
Route::get('/case/delete/{$id}','destroy')->middleware('auth');
});
HospitalCase.php controller
class HospitalCase extends Controller
{
function index()
{
echo 'index';
}
function create()
{
echo 'create';
}
function show($id)
{
echo 'show';
}
function store()
{
// validation rules
}
function edit($id)
{
return view('case/edit');
}
function update($id)
{
}
function destroy($id)
{
}
}
This is what I see on the browser:
I have been trying to figure this out for hours and can't think of what I'm doing wrong.
PS: The auth middleware is using laravel breeze (unmodified)
The reason it's showing the show route is because you defined
Route::get('/case/{id}','show')->middleware('auth');
before it, therefore, it's matching case/create as show('create')
Try defining the route afterwards.
Route::get('/case/create','create')->middleware('auth');
Route::post('/case/create','store')->middleware('auth');
Route::get('/case/{id}','show')->middleware('auth');
Just want to reiterate what #TimLewis has suggested, I think you need to put this route:
Route::get('/case/create','create')->middleware('auth');
Above this route:
Route::get('/case/{id}','show')->middleware('auth');
But you could try using Laravel’s route resource so you don’t need to write out all the routes -
use App\Http\Controllers\HospitalCaseController;
Route::resource('case', HospitalCaseController::class);
Suppose I have this controller:
class Site extends CI_Controller {
public function __constructor()
{
parent::__constructor();
echo 'test';
}
public function index()
{
$view['site'] = true;
$view['view_home'] = lang('home');
$view['view_home_url'] = base_url();
$view['view_name'] = lang('home');
$view['content'] = 'site/home';
$this->load->view('partials/template', $view);
}
public function association()
{
echo 'test';
}
}
when I type: http://localhost/mysite/association I get 404. Essentially I would like to use the same controller Site which is the default controller, to load multiple routers. How can I do this?
You are using codeigniter and you can't access url lik below as you haven't defined any routes for that. http://localhost/mysite/association . You will have to go url like
http://localhost/mysite/site/association //sometimes need index.php
if you don't configure it. first parameter is the controller and second one is a method
For going to your custom URL you will have to go to application/config/routes.php and define your new custom
route $route['association'] = 'site/association';
You can also define like below routes and access from browser
$route['association/url1/url2/url2'] = 'site/association'; //simple
Hope that you have got your answer
So I have a Laravel Controller (MainController.php) with the following lines:
...
public function _settings_a(){
return view('_settings_a');
}
public function _settings_b(){
return view('_settings_b');
}
public function _settings_c(){
return view('_settings_c');
}
public function _settings_d(){
return view('_settings_d');
}
public function _staff_a(){
return view('_staff_a');
}
public function _staff_b(){
return view('_staff_b');
}
public function _staff_c(){
return view('_staff_c');
}
...
And my routes.php is as follows:
Route::any('_staff_a''MainController#_staff_a');
Route::any('_staff_b''MainController#_staff_b');
...
etc.
It seems there are a LOT of lines and a LOT of things to change if I change my mind...
I was wondering if I can have some regex in routes.php and an equivalent regex in MainController.php for handling routes that begin with an underscore (_)?
Can any Laravel experts share some tips/suggestions? I'm quite new to the framework.
Sure - just add it as a parameter. E.g. like this:
Route::any('_staff_{version}', 'MainController#_staff');
public function _staff($version) {
return view('_staff_'.$version);
}
I don't think you need to mess with regex. You can use implicit controllers Route::controller() which isn't the BEST solution, but will do what I think you are wanting.
So instead of
Route::any(..)
you can do
Route::controller('url', 'MainController');
So your route to whatever 'url' is will send you to this controller. Follow that with a '/' and then add whichever method in the controller you want to call.
Here is an example:
My url: 'http://www.example.com/users'
// routes.php
Route::controller('users', UserController');
// UserController.php
public function getIndex()
{
// index stuff
}
Now I send a request like: http://www.example.com/users/edit-user/125
// UserController.php
public function getEditUser($user_id)
{
// getEditUser, postEditUser, anyEditUser can be called on /users/edit-user
// and 125 is the parameter pasted to it
}
Doing it this way should allow you to be able to just send a request (post or get) to a url and the controller should be able to call the correct method depending on the url.
Here are some more rules about it: http://laravel.com/docs/5.1/controllers#implicit-controllers
**Route**
Route::get('admin', function()
{
return View::make('theme-admin.main');
});
**Controller**
class Admin_Controller extends Base_Controller {
public function action_index()
{
echo __FUNCTION__;
}
If I forward request to controller, then I have to define View::make in every function in controller. If I don't forward it, action function doesn't work.
Should I just forward requests to controller and use View::make inside action functions or there are better alternatives?
Actually isn't necessary to define View::make in every function of your controllers.
You can, for example, execute an action and then redirect to another action, that could View::make.
Let's say you want to create an user and then show its profile, in a RESTful way. You could do:
# POST /users
public function user_create()
{
$user = User::create(...);
// after you have created the user, redirect to its profile
return Redirect::to_action('users#show', array($user->id));
// you don't render a view here!
}
# GET /users/1
public function get_show($id)
{
return View::make('user.show');
}
you can call the controller function like this
$app = app();
$controller = $app->make('App\Http\Controllers\EntryController');
return $controller->callAction('getEntry', $parameters = array());
or you can simply dispatch the request to another controller url
$request = \Request::create(route("entryPiont"), 'POST', array()));
return \Route::dispatch($request);
How can i forward to other action inside the same controller avoiding repeat all dispatch proccess ?
Example:
If i point to User Controller the default action is indexAction() inside this funciton i use _forwad('list') ... but all dispatch proccess are repeated.. and i dont that
Whats is the right way ?
Usually, you will install routes to redirect your users to the proper (default) action, instead of the index action (read how to redirect from a given route using Zend_Router). But you can do everything manually if you really want to (however this is called "writing hacker code to achieve something dirty") directly from the controller.
Change your "view script" to be rendered, then call your action method....
// inside your controller...
public function indexAction() {
$this->_helper->viewRenderer('foo'); // the name of the action to render instead
$this->fooAction(); // call foo action now
}
If you tend on using this "trick" often, perhaps you may write a base controller that you extend in your application, which can simply have a method like :
abstract class My_Controller_Action extends Zend_Controller_Action {
protected function _doAction($action) {
$method = $action . 'Action';
$this->_helper->viewRenderer($action);
return $this->$method(); // yes, this is valid PHP
}
}
Then call the method from your action...
class Default_Controller extends My_Controller_Action
public function indexAction() {
if ($someCondition) {
return $this->_doAction('foo');
}
// execute normal code here for index action
}
public function fooAction() {
// foo action goes here (you may even call _doAction() again...)
}
}
NOTE : this is not the official way to do it, but it is a solution.
We Can Also use this Helper To redirect
$this->_helper->redirector->gotoSimple($action, $controller, $module, $params);
$this->_helper->redirector->gotoSimple('edit'); // Example 1
$this->_helper->redirector->gotoSimple('edit', null, null, ['id'=>1]); // Example 2 With Params
If you don't want to re-dispatch there is no reason you can't simply call the action - it's just a function.
class Default_Controller extends My_Controller_Action
{
public function indexAction()
{
return $this->realAction();
}
public function realAction()
{
// ...
}
}
You could also create a route. For example I have in my /application/config/routes.ini a section:
; rss
routes.rss.route = rss
routes.rss.defaults.controller = rss
routes.rss.defaults.action = index
routes.rssfeed.route = rss/feed
routes.rssfeed.defaults.controller = rss
routes.rssfeed.defaults.action = index
Now you only need one action and that is index action but the requess rss/feed also goes there.
public function indexAction()
{
...
}